非常适合用来做以数据为中心的配置文件。冒号之后有一个空格)单引号''表示转义字符不生效,双引号""表示转义字符生效字面量:单个的、不可再分的值。可以是date、boolean、string、number、null
k: v
对象:键值对的集合。可以是map、hash、object
# 行内写法:
k: {k1: v1, k2: v2, k3: v3}
# 或
k:
k1: v1
k2: v2
k3: v3
数组:一组按次序排列的值。可以是array、list、set、queue
#行内写法:
k: [v1, v2, v3]
#或者
k:
- v1
- v2
- v3
经过测试,
yaml中的key不能为中文,而value可以。
@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;
}
@Data
public class Pet {
private String name;
private Double weight;
}
person:
user-name: zhangsan
boss: true
birth: 1996/06/14
age: 26
pet: {name: 胖胖}
interests: [篮球, 足球, DNF]
animal: [小狗, 小猫, 胖虎]
score:
chinese: {value: 99}
math:
value: 100
english:
value: 98
salarys: [18000.0, 25000.0]
all-pets:
sick: [{name: 小红}, {name: 小蓝}]
healthy:
- {name: 小白}
- {name: 小黑}
自定义的类和配置文件绑定一般没有提示。若要提示,添加如下依赖:
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-configuration-processorartifactId>
<optional>trueoptional>
dependency>
在打包的时候,可以把这个包排除,因为这个包只是为了在开发的时候能够有提示作用,对打包的结果没有影响。
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-configuration-processorartifactId>
exclude>
excludes>
configuration>
plugin>
plugins>
build>