yaml syntax format, the use of yaml in springboot
yaml
YAML (/ˈjæməl/, ending sound like camel camel) is a highly readable format for expressing data serialization. YAML references a variety of other languages, including: C, Python, Perl, and takes inspiration from XML, the data format of email (RFC 2822). Clark Evans first published this language in 2001, and Ingy dot Net and Oren Ben-Kiki are also co-designers of this language. Several programming or scripting languages currently support (or parse) this language.
YAML is a recursive acronym for "YAML Ain't a Markup Language" (YAML is not a markup language). When this language was developed, YAML actually meant: "Yet Another Markup Language" ( still a markup language ), but in order to emphasize that this language is data-centric rather than markup language-focused, and Rename with reverse acronym.
1.yaml syntax format
- key: value; there is a space between kv
- Case Sensitive
- Use indentation to indicate hierarchical relationships
- Tabs are not allowed for indentation, only spaces are allowed
- The number of spaces indented is not important, as long as the elements of the same level are left-aligned
'#'
Indicates a comment- The string does not need to be quoted. If you want to add it,
'单引号
it"双引号
means that the string content will be escaped/unescaped
2. Data type representation
- Literals: A single, indivisible value. date, boolean, string, number, null
k: v
- Object: A collection of key-value pairs. map, hash, set, object
行内写法: k: {
k1:v1,k2:v2,k3:v3}
#或
k:
k1: v1
k2: v2
k3: v3
- Array: A group of values arranged in order. array, list, ...
行内写法: k: [v1,v2,v3]
#或者
k:
- v1
- v2
- v3
The use of yaml in springboot
1. @ConfigurationProperties annotation and bean creation
@ConfigurationProperties
Annotation, used to bind the properties file or yml file configuration to the javabean component.
Create a Person bean component:
package com.robin.boot.bean;
import lombok.Data;
import lombok.ToString;
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") // 将application.yaml中前缀为person的数据绑定到person的bean组件中
@Component // 将person的javabean当作一个容器中的组件
@ToString
@Data
public class Person {
private String userName;
private Boolean boss;
private Date birth;
private Integer age;
private Pet pet;
private String[] hobbies;
private List<String> animal;
private Map<String, Object> score;
private Set<Double> pinMoney;
private Map<String, List<Pet>> allPets;
}
Create a dependent Pet class:
package com.robin.boot.bean;
import lombok.Data;
import lombok.ToString;
@ToString
@Data
public class Pet {
private String name;
private int age;
}
2. Write the controller
Use the @RequestMapping annotation to respond to the specified url path request mapping:
package com.robin.boot.controller;
import com.robin.boot.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 TestController {
@Autowired // 自动装配
Person person;
@RequestMapping("/test") // 路径请求映射
public Person person(){
return person;
}
}
3. Configure the application.yaml configuration file
Use the yaml syntax format to write and bind the corresponding Person attributes:
# yaml表示以上对象
person:
userName: robin
boss: true
birth: 2001/08/09
age: 21
pet:
name: 小白
age: 3
hobbies: [下棋,长跑,编程]
animal:
- 小狗
- 小猫
- 小猪
score: {
操作系统:88,数据结构:92,计网:97,计组:91}
pinMoney:
- 1500.01
- 2009.99
allPets:
sick:
- 小狗:
name: 小黄
age: 2
healthy:
- 小猫: {
name: 胖橘,age: 4}
- 小猪: {
name: 大花,age: 1}
4. Open yaml prompt in pom.xml
The pom.xml configuration prompted by lombok and yaml:
<!-- Lombok -->
<dependency>
<groupId>cn.itlym.shoulder</groupId>
<artifactId>lombok</artifactId>
<version>0.1</version>
</dependency>
<!-- 开启yml提示 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
Filter resource configuration-processor:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<!-- 将yml提示的configuration-processor 取消避免打包到jar中 -->
<configuration>
<excludes>
<exclude>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>