- 基本语法
- 数据类型
- 实例
yaml(ml,markup language),是一种标记语言。
基本语法- key: value,kv之间有空格。
- 大小写敏感。
- 使用缩进表示层级关系。
- 缩进的空格数不重要,只要相同层级的元素左对齐即可。
- #,表示注释。
- 字符串无需引号(不论单引号或双引号)。
- 字面量。如date、boolean、string、number,null。
k: v
- 对象。
k:
k1: v1
k2: v2
k3: v3
或
k: {k1:v1,k2:v2}
- 数组。
k:
- v1
- v2
- v3
或
k: [v1,v2,v3]实例
- 新建Spring项目:demo,自动生成了pom.xml和主程序类DemoApplication。
4.0.0 org.springframework.boot spring-boot-starter-parent 2.6.2 com.example demo 0.0.1-SNAPSHOT demo Demo project for Spring Boot 1.8 org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test test org.springframework.boot spring-boot-maven-plugin
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
- 修改pom.xml,添加依赖。
首先,添加依赖lombok。
org.projectlombok lombok
后续如果遇到IDEA提醒:Spring Boot Configuration Annotation Processor not configured,解决方案如下:
即添加依赖:spring-boot-configuration-processor。
org.springframework.boot spring-boot-configuration-processor
- 创建包com.example.demo.bean,并在该包下新建类:Person和Pet。
package com.example.demo.bean;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import org.springframework.boot.context.properties.ConfigurationProperties;
import java.util.Date;
import java.util.List;
import java.util.Map;
@NoArgsConstructor
@AllArgsConstructor
@Data
@ToString
@ConfigurationProperties(prefix = "person")
public class Person {
private String username;
private Date birth;
private Integer age;
private Boolean boss;
private String[] hobbies;
private List animals;
private Map scores;
private Pet pet;
private Map> allPets;
}
package com.example.demo.bean;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
@NoArgsConstructor
@AllArgsConstructor
@Data
@ToString
public class Pet {
private String name;
private String weight;
}
- 创建包com.example.demo.config,并在该包下新建配置类:MyConfig。
package com.example.demo.config;
import com.example.demo.bean.Person;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableConfigurationProperties(Person.class)
public class MyConfig {
}
- 创建包com.example.demo.controller,并在该包下新建控制器类:DemoController。
package com.example.demo.controller;
import com.example.demo.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 DemoController {
@Autowired
private Person person;
@RequestMapping("/person")
public Person person(){
return person;
}
}
- 重命名application.properties为application.yml(或者application.yaml)。
person:
username: 张三
birth: 1997/10/01
age: 24
boss: false
hobbies: [足球,篮球,乒乓球]
animals:
- 小苹果
- 小月亮
pet:
name: 小苹果
weight: 25
scores:
English: 80
Math: 90
all-pets:
health:
- {name:小苹果,weight:15}
- name: 小月亮
weight: 25
- 启动应用,访问接口localhost:8080/person。
返回的内容如下。
{
"username": "张三",
"birth": "1997-09-30T16:00:00.000+00:00",
"age": 24,
"boss": false,
"hobbies": [
"足球",
"篮球",
"乒乓球"
],
"animals": [
"小苹果",
"小月亮"
],
"scores": {
"English": 80,
"Math": 90
},
"pet": {
"name": "小苹果",
"weight": "25"
},
"allPets": {
"health": [
{
"name": "",
"weight": null
},
{
"name": "小月亮",
"weight": "25"
}
]
}
}



