java實現(xiàn)Yaml轉(zhuǎn)Json示例詳解
緣起
年前,因為項目需要進(jìn)行配置的優(yōu)化和架構(gòu)的升級,領(lǐng)導(dǎo)給我來了個任務(wù),讓我去進(jìn)行技術(shù)調(diào)研
需要將配置中心的yaml配置文件里面的配置轉(zhuǎn)為Json的形式,以便后面可以通過寫配置文件的方式,讓數(shù)據(jù)源變的可拔插
調(diào)研過程
本著有輪子就直接用,絕不自己造輪子的態(tài)度,我成功找到了 snakeyaml 這個依賴
<dependency> <groupId>org.yaml</groupId> <artifactId>snakeyaml</artifactId> <version>1.30</version> </dependency>
1.0 入口點
Yaml yaml = new Yaml();
注意??:此實現(xiàn)并不是一個線程安全的
1.1 基本用法
yml 文件:
common.cloud: discovery: consul config: consul
Java 代碼:
@Test
public void YAMLToJSON01() {
Yaml yaml = new Yaml();
InputStream resourceAsStream = this.getClass().getClassLoader().getResourceAsStream("yamlToJson.yml");
// 解析唯一的 yaml 文檔轉(zhuǎn)為 Java 對象
Map<String, Object> load = yaml.load(resourceAsStream);
JSONObject jsonObject = JSONObject.parseObject(load);
System.out.println(jsonObject.toString());
}
結(jié)果
{"common.cloud":{"discovery":"consul","config":"consul"}}
1.2 自定義類型解析
yml 文件:
cloud:
discovery: consul
config: consul
extension:
- data: application-oracle-public.yml
refresh: true
Java 代碼:
首先是創(chuàng)建對應(yīng)的實體類
@Data
public class Cloud {
private Contact cloud;
private List<Contact02> extension;
}
@Data
public class Contact {
private String discovery;
private String config;
}
@Data
public class Contact02 {
private String data;
private String refresh;
}
@Test
public void YAMLToJSON02() {
Yaml yaml = new Yaml(new Constructor(Cloud.class));
InputStream resourceAsStream = this.getClass()
.getClassLoader()
.getResourceAsStream("yamlToJson.yml");
// 解析 yaml 文檔轉(zhuǎn)為 Cloud 對象
Cloud cloud = yaml.load(resourceAsStream);
JSONObject jsonObject = JSONObject.parseObject(JSONObject.toJSONString(cloud));
System.out.println(jsonObject.toString());
}
結(jié)果
{"cloud":{"discovery":"consul","config":"consul"},"extension":[{"data":"application-oracle-public.yml","refresh":"true"}]}
1.3 實戰(zhàn)
1.3.1 從本地讀配置文件
在項目中,假如領(lǐng)導(dǎo)只需要 spring.cloud.nacos 這一塊的配置內(nèi)容,但我們把整個配置文件的內(nèi)容全部取出來了,那肯定是不符合領(lǐng)導(dǎo)需求的
于是就有了接下來的一些代碼,一些關(guān)鍵的注釋也標(biāo)注在了代碼中
yml 文件:
spring.cloud.nacos:
discovery:
server-addr: xxx
config:
server-addr: xxx
file-extension: yml
extension-configs:
- data-id: application-oracle-public.yml
refresh: true
eureka:
instance:
preferIpAddress: true
instanceId: ${spring.application.name}-${spring.cloud.client.ip-address}-${server.port}
client:
serviceUrl:
defaultZone: http://xxx/eureka/
Java 代碼:
@Test
public void YAMLToJSON01() {
Yaml yaml = new Yaml();
InputStream resourceAsStream = this.getClass().getClassLoader().getResourceAsStream("bootstrap.yml");
// 解析 yaml 文檔轉(zhuǎn)為 Map
Map<String, Object> load = yaml.load(resourceAsStream);
HashMap<String, Object> objectObjectHashMap = new HashMap<>();
// 通過 key 取到對應(yīng)的 value 然后放到新的 Map 中
objectObjectHashMap.put("spring.cloud.nacos", load.get("spring.cloud.nacos"));
JSONObject jsonObject = JSONObject.parseObject(JSONObject.toJSONString(objectObjectHashMap));
System.out.println(jsonObject.toString());
}
結(jié)果
{"spring.cloud.nacos":{"discovery":{"server-addr":"xxx"},"config":{"file-extension":"yml","server-addr":"xxx","extension-configs":[{"refresh":true,"data-id":"application-oracle-public.yml"}]}}}
下面的代碼是使用靜態(tài)內(nèi)部類的方式去保證線程的安全
public class YamlToJsonUtil implements Serializable {
private final static String BOOTSTRAP = "bootstrap-test.yml";
?
private final Yaml yaml = new Yaml(new SafeConstructor());
?
/**
* Yaml to json.
*
* @param key the key
* @return the json object
*/
public JSONObject YamlToJson(String key) {
YamlToJsonUtil instance = YamlToJsonInner.instance;
Yaml yaml = instance.getYaml();
InputStream resourceAsStream = null;
try {
resourceAsStream = this.getClass().getClassLoader().getResourceAsStream(BOOTSTRAP);
} catch (Exception e) {
throw new RuntimeException(e);
}
Map<String, Object> load = null;
Map<String, Object> result = new HashMap<>();
load = yaml.load(resourceAsStream);
result.put(key, load.get(key));
return JSONObject.parseObject(JSONObject.toJSONString(result));
}
?
private YamlToJsonUtil() {
?
}
?
/**
* Gets instance.
*
* @return the instance
*/
public static YamlToJsonUtil getInstance() {
return YamlToJsonInner.instance;
}
?
private static class YamlToJsonInner {
private static final YamlToJsonUtil instance = new YamlToJsonUtil();
}
?
/**
* Gets yaml.
*
* @return the yaml
*/
public Yaml getYaml() {
return yaml;
}
}
@Test
public void YAMLToJSON03() {
String key = "spring.cloud.nacos";
YamlToJsonUtil yamlToJsonUtil = YamlToJsonUtil.getInstance();
JSONObject jsonObject = yamlToJsonUtil.YamlToJson(key);
System.out.println(jsonObject);
}
到了這一步領(lǐng)導(dǎo)的需求基本已經(jīng)完成了一大半,接下來只需要再從配置中心中將這些配置取出來就可以摸魚下班了
那么問題來了:該如何從配置中心中找到那一份配置文件呢?
就在這時,我想起來了 Spring 里面的 org.springframework.core.env.PropertySource 這個類。 org.springframework.core.env.PropertySource 這一個類顧名思義就是屬性源的意思,再具體一點就是對獲取鍵值對資源的抽象類。我們可以從org.springframework.core.env.AbstractEnvironment#getPropertySources 這個方法中得到配置中心里面的配置以及環(huán)境配置,再通過過濾即可得到我們想到的數(shù)據(jù)
(ps:上面提到的這個類以及對應(yīng)的方法大家可以自行百度,因為篇幅等原因就不在這里詳解了)
1.3.2 從配置中心讀配置文件
接下來直接看代碼:
public Map<String, Object> getPropertyMap(Environment environment) {
// 得到當(dāng)前環(huán)境中所有的屬性集合
List<?> propertySourceCollection = ((StandardEnvironment) environment)
.getPropertySources().stream()
.map(PropertySource::getSource)
.collect(Collectors.toList());
List<LinkedHashMap<String, Object>> list = new ArrayList<>();
for (Object source : propertySourceCollection) {
// 判斷屬性類型是否為 LinkedHashMap
if (source instanceof LinkedHashMap) {
list.add((LinkedHashMap<String, Object>) source);
}
}
// 將集合中多個 LinkedHashMap 合并為一個 Map
return list.stream()
.flatMap(map -> map.entrySet().stream())
.collect(Collectors.toMap(
Map.Entry::getKey,
Map.Entry::getValue,
(value1, value2) -> value1));
}
經(jīng)過測試,上面的代碼可以將配置中心中的所有配置都存儲在一個 Map<String, Object> 中。
但是由于 yaml.load() 并沒有入?yún)?Map<String, Object> 的重載方法,所以我們還得引入這個依賴
<dependency>
<groupId>pl.jalokim.propertiestojson</groupId>
<artifactId>java-properties-to-json</artifactId>
<version>5.1.0</version>
</dependency>
這個依賴?yán)锩娴姆椒梢詫?Map 轉(zhuǎn)為 properties,再轉(zhuǎn)為 json,剛好滿足我們的需求。
Map<String, Object> propertyMap = getPropertyMap(environment); // 將 Map 轉(zhuǎn)為 Json String json = new PropertiesToJsonConverter().convertFromValuesAsObjectMap(propertyMap);
直接上整個從配置中心讀yaml配置文件里面的配置轉(zhuǎn)為Json的全部代碼
線程安全單例:
/**
* The type Yaml to json util.
*/
public class YamlToJsonForCloudUtil implements Serializable {
private final Yaml yaml = new Yaml(new SafeConstructor());
/**
* Yaml to json.
*
* @param key the key of the configuration file in Consul or Nacos
* @return the json object
*/
public JSONObject YamlToJson(String key) {
YamlToJsonForCloudUtil instance = YamlToJsonInner.instance;
Environment environment = instance.getEnvironment();
if (environment == null) {
throw new RuntimeException("Environment 為空!");
}
Map<String, Object> propertyMap = getPropertyMap(environment);
// 將 Map 轉(zhuǎn)為 Json
String json = new PropertiesToJsonConverter().convertFromValuesAsObjectMap(propertyMap);
Yaml yaml = instance.getYaml();
Map<String, Object> load = null;
Map<String, Object> result = new HashMap<>();
// 解析 String 中唯一的 YAML 文檔并轉(zhuǎn)為 Map
load = yaml.load(json);
result.put(key, load.get(key));
return JSONObject.parseObject(JSONObject.toJSONString(result));
}
/**
* Gets property map.
*
* @param environment the environment
* @return the property map
*/
public Map<String, Object > getPropertyMap(Environment environment) {
// 得到當(dāng)前環(huán)境中所有的屬性集合
List<?> propertySourceCollection = ((StandardEnvironment) environment)
.getPropertySources().stream()
.map(PropertySource::getSource)
.collect(Collectors.toList());
List<LinkedHashMap<String, Object>> list = new ArrayList<>();
for (Object source : propertySourceCollection) {
// 判斷屬性類型是否為 LinkedHashMap
if (source instanceof LinkedHashMap) {
list.add((LinkedHashMap<String, Object>) source);
}
}
// 將集合中多個 LinkedHashMap 合并為一個 Map
return list.stream()
.flatMap(map -> map.entrySet().stream())
.collect(Collectors.toMap(
Map.Entry::getKey,
Map.Entry::getValue,
(value1, value2) -> value1));
}
private YamlToJsonForCloudUtil() {
}
/**
* Gets instance.
*
* @return the instance
*/
public static YamlToJsonForCloudUtil getInstance() {
return YamlToJsonInner.instance;
}
private static class YamlToJsonInner {
private static final YamlToJsonForCloudUtil instance = new YamlToJsonForCloudUtil();
}
/**
* Gets yaml.
*
* @return the yaml
*/
public Yaml getYaml() {
return yaml;
}
/**
* Gets environment.
*
* @return the environment
*/
public Environment getEnvironment() {
return SpringContextUtil.getBean(Environment.class);
}
}
工具類:
@Component
public class SpringContextUtil implements ApplicationContextAware {
private static ApplicationContext applicationContext;
/**
* 實現(xiàn)ApplicationContextAware接口的回調(diào)方法,設(shè)置上下文環(huán)境
*/
@Override
public void setApplicationContext(@NotNull ApplicationContext applicationContext){
SpringContextUtil.applicationContext = applicationContext;
}
/**
* 獲得spring上下文
* @return ApplicationContext spring上下文
*/
public static ApplicationContext getApplicationContext(){
return applicationContext;
}
/**
* 獲取bean
* @param name service注解方式name為小駝峰格式
* @return Object bean的實例對象
*/
public static Object getBean(String name) throws BeansException {
return getApplicationContext().getBean(name);
}
public static <T> T getBean(Class<T> className) {
return getApplicationContext().getBean(className);
}
/**
* 通過接口類型,返回所有實現(xiàn)了這個接口的實現(xiàn)類
*
* @param clazz
* @param <T>
* @return
*/
public static <T> Map<String, T> getBeansOfType(Class<T> clazz) {
return getApplicationContext().getBeansOfType(clazz);
}
}
測試:
@Test
public void YamlToJsonEnvironment() {
YamlToJsonForCloudUtil yamlToJsonUtil = YamlToJsonForCloudUtil.getInstance();
JSONObject dom = yamlToJsonUtil.YamlToJson("dom");
System.out.println(JSONObject.toJSONString(dom));
}以上就是java實現(xiàn)Yaml轉(zhuǎn)Json示例詳解的詳細(xì)內(nèi)容,更多關(guān)于java實現(xiàn)Yaml轉(zhuǎn)Json的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
javax.validation.constraints如何校驗參數(shù)合法性
本文將深入探討javax.validation.constraints的基本用法和高級應(yīng)用,幫助讀者更好地理解和運用這個強大的校驗框架,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-07-07
Mybatis關(guān)于動態(tài)排序 #{} ${}問題
這篇文章主要介紹了Mybatis關(guān)于動態(tài)排序 #{} ${}問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-10-10
使用java自帶des加密算法實現(xiàn)文件加密和字符串加密
這篇文章主要介紹了使用java自帶des加密算法實現(xiàn)文件加密和字符串加密的示例,需要的朋友可以參考下2014-03-03
springboot使用redis緩存亂碼(key或者value亂碼)的解決
在通過springboot緩存數(shù)據(jù)的時候,發(fā)現(xiàn)key是一堆很不友好的東西,本文主要介紹了springboot使用redis緩存亂碼(key或者value亂碼)的解決,感興趣的可以了解一下2023-11-11
Idea之沒有網(wǎng)絡(luò)的情況下創(chuàng)建SpringBoot項目的方法實現(xiàn)
本文主要介紹了Idea之沒有網(wǎng)絡(luò)的情況下創(chuàng)建SpringBoot項目的方法實現(xiàn),文中通過圖文介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-09-09

