尚硅谷:雷丰阳

Spring Initailizr

项目初始化向导

1
2
3
4
5
6
7
8
9
@RestController
public class FirstController {


    @RequestMapping("/hello")
    public String handle01(){
        return "Hello, Spring Boot 2!";
    }
}

yaml

配置文件

示例

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package com.example.helloworld.bean;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;

@ConfigurationProperties(prefix = "person")//和配置文件中的person前缀绑定
@Component//放到容器中
@Data
public class Person {

    private String userName;
    private Boolean boss;
    private Date birth;
    private Integer age;
    private Pet pet;
    private String[] interests;
    private List<String> animal;
    private Map<String, Object> score;
    private Set<Double> salarys;
    private Map<String, List<Pet>> allPets;
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package com.example.helloworld.bean;

import lombok.Data;

//@Data
public class Pet {
    private String name;
    private Double weight;
    
    //不用@Data可以右键generate
    //无参
    public Pet() {
    }
    
    //有参
    public Pet(String name, Double weight) {
        this.name = name;
        this.weight = weight;
    }
    
    //get,set
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Double getWeight() {
        return weight;
    }

    public void setWeight(Double weight) {
        this.weight = weight;
    }
    
    //tostring
    @Override
    public String toString() {
        return "Pet{" +
                "name='" + name + '\'' +
                ", weight=" + weight +
                '}';
    }
}

application.yml

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# 给自定义的类赋值
person:
  userName: zhangsan
  # 也可以加单引号和双引号,
  # 若为'zhangsan \n lisi'会将\n作为字符串输出
  # 若为"zhangsan \n lisi"会将\n作为换行输出
  # 单引号转义不生效,双引号转义字符生效
  boss: false
  birth: 2019/12/12 20:12:33
  age: 18
  pet:
    name: tomcat
    weight: 23.4
  interests: [篮球,游泳]

  animal:
    - jerry
    - mario
  score:
    english:
      first: 30
      second: 40
      third: 50
    math: [131,140,148]
    chinese: {first: 128,second: 136}
  salarys: [3999,4999.98,5999.99]
  allPets:
    sick:
      - {name: tom}
      - {name: jerry,weight: 47}
    health: [{name: mario,weight: 47}]

测试

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
package com.example.helloworld.controller;

import com.example.helloworld.bean.Person;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {
    @Autowired
    Person person;

    @RequestMapping("/person")
    public Person person(){
        return person;
    }
}

//{"userName":"zhangsan","boss":false,"birth":"2019-12-12T12:12:33.000+00:00","age":18,"pet":{"name":"tomcat","weight":23.4},"interests":["篮球","游泳"],"animal":["jerry","mario"],"score":{"english":{"first":30,"second":40,"third":50},"math":{"0":131,"1":140,"2":148},"chinese":{"first":128,"second":136}},"salarys":[3999.0,4999.98,5999.99],"allPets":{"sick":[{"name":"tom","weight":null},{"name":"jerry","weight":47.0}],"health":[{"name":"mario","weight":47.0}]}}

自定义类绑定的配置提示->添加依赖

1
2
3
4
5
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-configuration-processor</artifactId>
    <optional>true</optional>
</dependency>

防止打包的时候打进去不必要的东西,好像更新的版本可以自己排除

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
	<build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.springframework.boot</groupId>
                            <artifactId>spring-boot-configuration-processor</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>