Spring Boot QuickStart (i)

Source: Internet
Author: User

1. Installation and use of IDE tools
    • 1.1. Download IDE

      • Spring Tool Suite
      • Eclipse
    • 1.2. Introduction

      • MAVEN is one of the prerequisites for learning Spring boot
      • Spring Tool Suite to quickly build a spring boot project
    • 1.3. Spring Boot Project Structure preview
2, Restfull API Simple Project Quick Build
  • 2.1. Build a simple Restfull API interface Project

  • 2.2. Introduction of Spring-boot-starter-web

  • 2.3. Introduction of Spring-boot-devtools

  • 2.4, the implementation of the Code

    User.java:

    import java.util.Date;/** * 实体类 */public class User {    private int id;    private String name;    private Date date;    public int getId() {        return id;    }    public void setId(int id) {        this.id = id;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public Date getDate() {        return date;    }    public void setDate(Date date) {        this.date = date;    }}

    Indexcontroller.java:

    Import Java.util.date;import Java.util.hashmap;import Org.springframework.web.bind.annotation.pathvariable;import Org.springframework.web.bind.annotation.requestmapping;import Org.springframework.web.bind.annotation.requestparam;import Org.springframework.web.bind.annotation.restcontroller;import com.roncoo.education.bean.user;/** *    spring-boot-demo-2-1 */@RestController @requestmapping (value = "/index") public class Indexcontroller {@RequestMapping    Public String Index () {return ' Hello World '; }//@RequestParam a simple type of binding that can come out get and post @RequestMapping (value = "/get") public hashmap<string, object> get (        @RequestParam String name) {hashmap<string, object> map = new hashmap<string, object> ();        Map.put ("title", "Hello World");        Map.put ("name", name);    return map; }//@PathVariable get the dynamic parameter in the request URL @RequestMapping (value = "/get/{id}/{name}") Public User GetUser (@PathVariable in      T ID, @PathVariable String name) {  User user = new user ();        User.setid (ID);        User.setname (name);        User.setdate (New Date ());    return user; }}
  • 2.5. Running the project

    • Run the main method directly or use the MAVEN command:spring-boot:run
    • Test:http://localhost:8080/index
    • With parameters:http://localhost:8080/index/get?name=wujing
    • With the parameters are in Chinese:http://localhost:8080/index/get?name=测试
    • URL test:http://localhost:8080/index/get/1/wujing
    • URL test:http://localhost:8080/index/get/1/测试
  • 2.6. Packing
    • Command: clean package

    • To run the command:java –jar roncoo-education-0.0.1-SNAPSHOT.jar
3. Detailed configuration file: Properties and Yaml
  • 3.1, the configuration file effective order, the value will be overwritten:

    • 1. @TestPropertySource annotations
    • 2. Command-line arguments
    • 3.Java System Properties ( System.getProperties() )
    • 4. Operating system Environment variables
    • 5. Only the random.* attributes contained within will produce aRandomValuePropertySource
    • 6. Application configuration file (with application.properties Yaml and profile variables) outside the packaged jar
    • 7. Application configuration file ( application.properties containing YAML and profile variables) within the packaged jar
    • 8. @Configuration annotations on the class @PropertySource
    • 9. Default properties (using SpringApplication.setDefaultProperties specified)
  • 3.2, configure the random value (custom, only restart the project will regenerate random values)

    roncoo.secret=${random.value}roncoo.number=${random.int}roncoo.bignumber=${random.long}roncoo.number.less.than.ten=${random.int(10)}roncoo.number.in.range=${random.int[1024,65536]}
    • Read using annotations:@Value(value = "${roncoo.secret}")
    • Note: The yellow dot hint is to prompt for configuration metadata, you can not configure
  • 3.3. Attribute placeholders

    When values in Application.properties are used, they are filtered by the presence of environment, so you can refer to previously defined values (for example, System Properties).

    roncoo.name=www.roncoo.comroncoo.desc=${roncoo.name} is a domain name
  • 3.4, Application Property files, sorted by priority, high position will cover the low position

    • 1. A subdirectory under the current directory /config
    • 2. Current directory
    • 3. One classpath under the /config package
    • 4. classpath Root Path ( root -Default build path)
    • This list is sorted by priority (the high position in the list will have a low coverage position)
  • 3.5. Introduction to configuring Application ports and other configurations

    #端口配置:server.port=8090#时间格式化spring.jackson.date-format=yyyy-MM-dd HH:mm:ss#时区设置(若使用jackson格式化时间字符串需设置本地时区,默认是美国的时区)spring.jackson.time-zone=Asia/Chongqing
  • 3.6. Use Yaml instead of properties
    • Note: Add a space after the colon
4. Configuration file-Multi-environment configuration
    • 4.1. Benefits of multi-environment configuration

      • 1. Different environment configuration can be configured with different parameters
      • 2. Easy to deploy, improve efficiency, reduce errors
    • 4.2, the properties of multi-environment configuration

      • 1. Configure activation options
        • spring.profiles.active=dev
      • 2. Add additional Profiles
    • 4.3. YAML Multi-environment configuration
      ① Configuring activation options

      spring:  profiles:    active: dev

      ② add three English-language dashes in the configuration file to differentiate between different files

      ---spring:  profiles: dev
    • 4.4. Comparison of two configuration methods

      • 1.Properties Configure multi-environment, need to add multiple configuration files, Yaml only need one accessory file
      • 2. The difference in writing format, Yaml relatively concise, elegant
      • Disadvantages of 3.YAML: cannot be @PropertySource loaded by annotations. If you need to @PropertySource load values using annotations, use the properties file.
    • 4.5. Operation and use
      • java -jar myapp.jar --spring.profiles.active=dev
5. List of acknowledgements
    • Feng Yongwei

Spring Boot QuickStart (i)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.