SpringBootyaml配置List数组对象并映射简介
最近有这样⼀个需求
1. 在yml⽂件动态配置项⽬名称与ID (,这⾥不讲述)
2. 因为列表数量很少,⼜会改动,⼜不想存数据库单独建个表维护,故通过配置⽂件的项⽬列表信息l 配置
新增/删除/更改 list 即可
projects:
list:
- name: project_one
projectId: project_id_111111
- name: project_two
projectId: project_id_222222
- name: your_name
projectId: your_id
配置类
@ConfigurationProperties(prefix = "projects")
@Configuration
@ConfigurationProperties(prefix = "projects")
public class Projects {
public static List<Map<String, String>> list;  //static 才能拿配置值
public static List<Map<String, String>> getList() {
return list;
}
spring怎么读取yaml
public void setList(List<Map<String, String>> list) {
Projects.list = list;
}
}
接⼝
@GetMapping("/projects")
public Result<Object> projectList() {
List<Map<String, String>> list = Map();
return Result.ok(list);
}
响应效果
{
"success": true,
"message": "操作成功!",
"code": 200,
"result": [
{
"name": "project_one",
"projectId": "project_id_111111"
},
{
"projectId": "project_id_222222",
"name": "project_two"
},
{
"projectId": "your_id",
"name": "your_name"
}
],
"timestamp": 1601342384895
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。