如何进行springBoot yml文件的list读取问题总结,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。
折腾了很久,记录下。
# 自定义数据上报信息
xx:
# 机组信息
machine1s:
- name: XXXX
frequency: null
frequency-unit: null
pressure: 0.01
pressure-unit: Pa
flow: 0
flow-unit: NM3/H
state: 停机
runtime: 17.5
runtime-unit: 天
- name: XXXX
frequency: 42.4
frequency-unit: HZ
pressure: 0.39
pressure-unit: Pa
flow: 4730
flow-unit: NM3/H
state: 运行
runtime: 12.5
runtime-unit: 天
- name: XXXX
frequency: 46.4
frequency-unit: HZ
pressure: 0.00
pressure-unit: Pa
flow: 0
flow-unit: NM3/H
state: 停机
runtime: 8.2
runtime-unit: 天
- name: XXXX
frequency: 41.4
frequency-unit: HZ
pressure: 0.39
pressure-unit: Pa
flow: 9532
flow-unit: NM3/H
state: 运行
runtime: 3.2
runtime-unit: 天
- name: XXXX
frequency: null
frequency-unit: null
pressure: 0.38
pressure-unit: Pa
flow: 4800
flow-unit: NM3/H
state: 停机
runtime: 20.4
runtime-unit: 天
- name: XXXX
frequency: null
frequency-unit: null
pressure: 0.01
pressure-unit: Pa
flow: 0
flow-unit: NM3/H
state: 停机
runtime: 7.5
runtime-unit: 天
@ConfigurationProperties(prefix = "xx")
public class TXMachinesProperties {
private List<Map<String, String>> machine1s;
public List<Map<String, String>> getMachine1s() {
return machine1s;
}
public void setMachine1s(List<Map<String, String>> machine1s) {
this.machine1s = machine1s;
}
}
注意:
a.这里prefix写到接收对象的前一级即可;
b.这里的变量名必须要与配置的名称一致,才可自动接收。
这里其实可与上面的配置类写在一起,但是一个类就是做一件事情,就做了隔离。
@Configuration
@EnableConfigurationProperties({TXMachinesProperties.class})
public class TXMachinesConfig {
}
采用下面的方式即可。这里注意,由于使用这个yml的注解是属于SpringBoot的框架内进行的,因此这个属性注入只能在标有Spring的注解的类的范围内使用,不能在普通类使用。
@Autowired
private TXWorkShopAlarmProperties txWorkShopAlarmProperties;
可以解决了~
对象:键值对的集合,又称为映射(mapping)/ 哈希(hashes) / 字典(dictionary)
数组:一组按次序排列的值,又称为序列(sequence) / 列表(list)
纯量(scalars):单个的、不可再分的值
yml里的list配置(以 - 开头的行表示构成一个数组:):
weixin:
configs:
- schId: 111
appId: 11111
appSecret: 11111
templateId: 111111
- schId: 2222
appId: 222222
appSecret: 2222222
templateId: 2222222
导入jar包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<version>2.2.1.RELEASE</version>
</dependency>
写一个微信配置的实体类,将配置文件中配置的每一个属性的值,映射到这个实体类中
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.util.List;
@Component
@Data
@ConfigurationProperties(prefix = "weixin")
public class WxConfig {
private List<Config> configs;
@Data
public static class Config {
private Integer schId;
private String appId;
private String appSecret;
private String templateId;
}
}
1.注入该实体类
@Autowired
private WxConfig wxConfig;
2.解析,我这里是将其转为学校id为key的map,方便我后面使用,按照自己想要的读取就好了JSON.toJSONString(wxConfig)
public Map<Integer,Map> getWeiXinConfig(){
JSONObject o = JSON.parseObject(JSON.toJSONString(wxConfig));
JSONArray jsonArray = o.getJSONArray("configs");
Map<Integer,Map> map = new HashMap<>();
if (jsonArray.size() != 0) {
for (int j = 0; j < jsonArray.size(); j++) {
Map map2 = new HashMap();
JSONObject o1 = jsonArray.getJSONObject(j);
String appId = o1.getString("appId");
String appSecret = o1.getString("appSecret");
Integer schId = o1.getIntValue("schId");
String templateId = o1.getString("templateId");
map2.put("appId", appId);
map2.put("appSecret", appSecret);
map2.put("schId", schId);
map2.put("templateId", templateId);
map.put(schId,map2);
}
}
return map;
}
调用方法getWeiXinConfig(),输出map的输出结果:
关于如何进行springBoot yml文件的list读取问题总结问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注亿速云行业资讯频道了解更多相关知识。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。