1.pom.xml
<!--web项目jar包-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--set、get、tostring、slf4j日志、无参有参-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!--此包为,自动提示。为了让自定义的组件放入容器,在配置文件yml中使用的时候提示-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
2.bean
@ConfigurationProperties (prefix = "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; //map双列集合泛型(字符串,对象)
private Set<Double> salarys; //set单列集合泛型双精度
private Map<String, List<Pet>> allPets; //map双列集合泛型(字符串,链表泛型自定义对象)
}
@Data
public class Pet {
private String name;
private Double weight;
}
@ConfigurationProperties:扫描配置文件前缀prefix
@Component:声明该类为一个组件
3.application.yml
person:
username: "zhangsan \n lisi" #从yml层面看,单引号转义,双引号不转义
boss: true
birth: 2019/09/06
age: 18
pet: {name: 大黄,weigth: 66.3}
interests: [篮球,足球,排球]
animal: [大华,大白,大闲]
score: {xx: 99,xxx: 88} #key不能是中文
salarys: [888.22,55.55,66.55]
allPets:
sick: [{name: 大黄,weight: 66.3},{name: 大黑,weight: 66.33}]
health: [{name: 大白, weight: 66.33},{name: 大胖,weight: 66.33}]
4.controller
@RestController
public class TestController {
@Autowired
Person person;
@RequestMapping("person")
public Person getPersonDate(){
return person;
}
}
@RestController:复合注解,@Controller和@ResponseBody封装
@Autowired:注入容器中的组件
5.psotman测试接口