java實(shí)現(xiàn)Yaml轉(zhuǎn)Json示例詳解
緣起
年前,因?yàn)轫?xiàng)目需要進(jìn)行配置的優(yōu)化和架構(gòu)的升級(jí),領(lǐng)導(dǎo)給我來(lái)了個(gè)任務(wù),讓我去進(jìn)行技術(shù)調(diào)研
需要將配置中心的yaml配置文件里面的配置轉(zhuǎn)為Json的形式,以便后面可以通過(guò)寫(xiě)配置文件的方式,讓數(shù)據(jù)源變的可拔插
調(diào)研過(guò)程
本著有輪子就直接用,絕不自己造輪子的態(tài)度,我成功找到了 snakeyaml
這個(gè)依賴
<dependency> <groupId>org.yaml</groupId> <artifactId>snakeyaml</artifactId> <version>1.30</version> </dependency>
1.0 入口點(diǎn)
Yaml yaml = new Yaml();
注意??:此實(shí)現(xiàn)并不是一個(gè)線程安全的
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 對(duì)象 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)建對(duì)應(yīng)的實(shí)體類
@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 對(duì)象 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 實(shí)戰(zhàn)
1.3.1 從本地讀配置文件
在項(xiàng)目中,假如領(lǐng)導(dǎo)只需要 spring.cloud.nacos
這一塊的配置內(nèi)容,但我們把整個(gè)配置文件的內(nèi)容全部取出來(lái)了,那肯定是不符合領(lǐng)導(dǎo)需求的
于是就有了接下來(lái)的一些代碼,一些關(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<>(); // 通過(guò) key 取到對(duì)應(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)完成了一大半,接下來(lái)只需要再?gòu)呐渲弥行闹袑⑦@些配置取出來(lái)就可以摸魚(yú)下班了
那么問(wèn)題來(lái)了:該如何從配置中心中找到那一份配置文件呢?
就在這時(shí),我想起來(lái)了 Spring 里面的 org.springframework.core.env.PropertySource
這個(gè)類。 org.springframework.core.env.PropertySource
這一個(gè)類顧名思義就是屬性源的意思,再具體一點(diǎn)就是對(duì)獲取鍵值對(duì)資源的抽象類。我們可以從org.springframework.core.env.AbstractEnvironment#getPropertySources
這個(gè)方法中得到配置中心里面的配置以及環(huán)境配置,再通過(guò)過(guò)濾即可得到我們想到的數(shù)據(jù)
(ps:上面提到的這個(gè)類以及對(duì)應(yīng)的方法大家可以自行百度,因?yàn)槠仍蚓筒辉谶@里詳解了)
1.3.2 從配置中心讀配置文件
接下來(lái)直接看代碼:
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); } } // 將集合中多個(gè) LinkedHashMap 合并為一個(gè) Map return list.stream() .flatMap(map -> map.entrySet().stream()) .collect(Collectors.toMap( Map.Entry::getKey, Map.Entry::getValue, (value1, value2) -> value1)); }
經(jīng)過(guò)測(cè)試,上面的代碼可以將配置中心中的所有配置都存儲(chǔ)在一個(gè) Map<String, Object>
中。
但是由于 yaml.load()
并沒(méi)有入?yún)?Map<String, Object>
的重載方法,所以我們還得引入這個(gè)依賴
<dependency> <groupId>pl.jalokim.propertiestojson</groupId> <artifactId>java-properties-to-json</artifactId> <version>5.1.0</version> </dependency>
這個(gè)依賴?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);
直接上整個(gè)從配置中心讀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); } } // 將集合中多個(gè) LinkedHashMap 合并為一個(gè) 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; /** * 實(shí)現(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的實(shí)例對(duì)象 */ public static Object getBean(String name) throws BeansException { return getApplicationContext().getBean(name); } public static <T> T getBean(Class<T> className) { return getApplicationContext().getBean(className); } /** * 通過(guò)接口類型,返回所有實(shí)現(xiàn)了這個(gè)接口的實(shí)現(xiàn)類 * * @param clazz * @param <T> * @return */ public static <T> Map<String, T> getBeansOfType(Class<T> clazz) { return getApplicationContext().getBeansOfType(clazz); } }
測(cè)試:
@Test public void YamlToJsonEnvironment() { YamlToJsonForCloudUtil yamlToJsonUtil = YamlToJsonForCloudUtil.getInstance(); JSONObject dom = yamlToJsonUtil.YamlToJson("dom"); System.out.println(JSONObject.toJSONString(dom)); }
以上就是java實(shí)現(xiàn)Yaml轉(zhuǎn)Json示例詳解的詳細(xì)內(nèi)容,更多關(guān)于java實(shí)現(xiàn)Yaml轉(zhuǎn)Json的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
javax.validation.constraints如何校驗(yàn)參數(shù)合法性
本文將深入探討javax.validation.constraints的基本用法和高級(jí)應(yīng)用,幫助讀者更好地理解和運(yùn)用這個(gè)強(qiáng)大的校驗(yàn)框架,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-07-07java中實(shí)現(xiàn)token過(guò)期失效超時(shí)
在Java應(yīng)用程序中,為了確保安全性和保護(hù)用戶數(shù)據(jù),一種常見(jiàn)的做法是使用Token進(jìn)行身份驗(yàn)證和授權(quán),Token是由服務(wù)器生成的具有一定時(shí)效的令牌,用于識(shí)別和驗(yàn)證用戶身份,當(dāng)Token失效后,用戶將無(wú)法再進(jìn)行相關(guān)操作,從而提高系統(tǒng)的安全性2023-10-10Mybatis關(guān)于動(dòng)態(tài)排序 #{} ${}問(wèn)題
這篇文章主要介紹了Mybatis關(guān)于動(dòng)態(tài)排序 #{} ${}問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10使用java自帶des加密算法實(shí)現(xiàn)文件加密和字符串加密
這篇文章主要介紹了使用java自帶des加密算法實(shí)現(xiàn)文件加密和字符串加密的示例,需要的朋友可以參考下2014-03-03springboot使用redis緩存亂碼(key或者value亂碼)的解決
在通過(guò)springboot緩存數(shù)據(jù)的時(shí)候,發(fā)現(xiàn)key是一堆很不友好的東西,本文主要介紹了springboot使用redis緩存亂碼(key或者value亂碼)的解決,感興趣的可以了解一下2023-11-11Idea之沒(méi)有網(wǎng)絡(luò)的情況下創(chuàng)建SpringBoot項(xiàng)目的方法實(shí)現(xiàn)
本文主要介紹了Idea之沒(méi)有網(wǎng)絡(luò)的情況下創(chuàng)建SpringBoot項(xiàng)目的方法實(shí)現(xiàn),文中通過(guò)圖文介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-09-09