pom依赖
<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
<version>1.17</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-yaml</artifactId>
<version>2.9.7</version>
</dependency>
工具代码
package com.donfaquir.axe.zk.yaml;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.yaml.snakeyaml.Yaml;
import java.util.Map;
/**
* @author:
* @description: 使用snakeyaml框架读取yaml文件
* @date Create in 2018/12/28 15:08
* @modified By:
*/
@SuppressWarnings("unchecked")
public class SnakeYaml {
private static Logger log = LoggerFactory.getLogger(SnakeYaml.class);
/** 查询路径分隔符,类似:dubbo.registry.address */
private static final String SEPARATOR = ".";
/** 文件路径 */
private String filePath;
/** yaml文件操作入口 */
private Yaml yaml;
/** 标识是否创建Yaml实例 */
private Boolean isInit;
public SnakeYaml(String filePath){
this.filePath = filePath;
this.yaml = new Yaml();
this.isInit = true;
}
/**
* 根据key值路径,获取指定字符串的值
* <p>
* 在yaml文件中,形如:
* <blockquote><pre>
* first:
* second:
* third: value
* </pre></blockquote>
* 可以通过下面的调用方式获取"value"
* <blockquote><pre>
* String str = get("first.second.third");
* </pre></blockquote>
* 最多支持层级为五层;更深层的数据无法通过该方法获取,会返回key路径。
* 层级高于五层,可以调用 {@link SnakeYaml#getYamlObject()} 获取配置文件得Object对象,手动封装获取
*
* @param keyPath 取值的key路径
* @return 返回对应的值
*/
public Object get(String keyPath){
if(null == keyPath || !this.isInit){
return null;
}
//如果给定的取值路径是多级路径,
//只支持最多五级路径
String[] keys;
if(keyPath.contains(SEPARATOR)){
keys = keyPath.split("\\.");
}else{
return getFirstOrderValue(keyPath);
}
return getValue(yaml.load(this.getClass().getClassLoader().getResourceAsStream(this.filePath)), keys, keys.length);
}
/**
* 获取值
* 如果key路径中只有一层,直接获取值
* 如果key路径中有多层,会多次调用该方法,判断路径中的key是否有对应的值,也就是进行非空判断
* @param load yaml文件加载对象
* @param keys key路径中的值组成的数组
* @param step key路径中的层数
* @return 获取的值
*/
private Object getValue(Object load,String[] keys,int step){
if(null == load || null == keys){
return null;
}
if(step > keys.length){
return null;
}
switch (step){
case 1:
Map<String, Object> load1 = (Map<String, Object>) load;
return load1.get(keys[0]);
case 2:
Object load22 = getValue(load,keys,1);
if(null != load22){
Map<String, Object> load2 = (Map<String, Object>) load22;
return load2.get(keys[1]);
}
return null;
case 3:
Object load33 = getValue(load,keys,2);
if(null != load33){
Map<String, Object> load3 = (Map<String, Object>) load33;
return load3.get(keys[2]);
}
return null;
case 4:
Object load44 = getValue(load,keys,3);
if(null != load44){
Map<String, Object> load4 = (Map<String, Object>) load44;
return load4.get(keys[3]);
}
return null;
case 5:
Object load55 = getValue(load,keys,4);
if(null != load55){
Map<String, Object> load5 = (Map<String, Object>) load55;
return load5.get(keys[4]);
}
return null;
default:
return null;
}
}
/**
* 获取yaml文件中,第一层key对应的值
* @param keyName key值
* @return 对应的value
*/
public Object getFirstOrderValue(String keyName){
if(keyName == null){
return null;
}
if(keyName.contains(SEPARATOR)){
this.get(keyName);
}
Map<String, Object> load = (Map<String, Object>) yaml.load(this.getClass().getClassLoader().getResourceAsStream(this.filePath));
return load == null ? null : load.get(keyName);
}
/**
* 如果获取数据的层级大于5级,调用此方法获取文件的Object对象
* 用户手动获取对应数据
* @return 文件的Object对象
*/
public Object getYamlObject(){
return yaml.load(this.getClass().getClassLoader().getResourceAsStream(this.filePath));
}
/**
* 封装成指定的实例对象
* @param clazz 实例类型
* @param <T> 占位符
* @return 实例对象
*/
public <T> T getObjectsFromYaml(Class<T> clazz){
return yaml.loadAs(this.getClass().getClassLoader().getResourceAsStream(this.filePath), clazz);
}
}