Java Yml格式轉(zhuǎn)換為Properties問題
問題引入
使用在線的yml轉(zhuǎn)換properties, 發(fā)現(xiàn)有屬性內(nèi)容漏了,網(wǎng)站地址https://tooltt.com/yaml2properties/。
于是自己動手寫個(gè)轉(zhuǎn)換工具類,自測過多個(gè) yml 文件,目前沒發(fā)現(xiàn)遺漏的。
需要轉(zhuǎn)換的yaml文件如下
spring: application: name: xtoon-sys-server cloud: nacos: config: server-addr: localhost:8848 file-extension: yaml enabled: true boot: admin: client: url: http://localhost:5001 username: admin password: admin instance: prefer-ip: true management: health: redis: enabled: false endpoint: health: show-details: always endpoints: web: exposure: include: "*"
在線轉(zhuǎn)換網(wǎng)站轉(zhuǎn)換的結(jié)果
spring.application.name=xtoon-sys-server spring.cloud.nacos.config.server-addr=localhost:8848 spring.cloud.nacos.config.file-extension=yaml spring.boot.admin.client.url=http://localhost:5001 spring.boot.admin.client.username=admin spring.boot.admin.client.password=admin management.endpoint.health.show-details=always management.endpoints.web.exposure.include=*
正確的轉(zhuǎn)換結(jié)果應(yīng)該如下
spring.application.name=xtoon-sys-server spring.cloud.nacos.config.server-addr=localhost:8848 spring.cloud.nacos.config.file-extension=yaml spring.cloud.nacos.config.enabled=true spring.boot.admin.client.url=http://localhost:5001 spring.boot.admin.client.username=admin spring.boot.admin.client.password=admin spring.boot.admin.client.instance.prefer-ip=true management.health.redis.enabled=false management.endpoint.health.show-details=always management.endpoints.web.exposure.include=*
在線網(wǎng)站轉(zhuǎn)換結(jié)果截圖如下
對比原始文本和轉(zhuǎn)換結(jié)果,發(fā)現(xiàn)少了幾個(gè)屬性
spring.cloud.nacos.config.enabled=true spring.boot.admin.client.instance.prefer-ip=true management.health.redis.enabled=false
這幾個(gè)結(jié)果有些特征,value值是boolean類型的。不知道還有沒有其它類型的數(shù)據(jù)會有遺漏的?
轉(zhuǎn)換代碼
導(dǎo)入yaml讀取jar
<dependency> <groupId>org.yaml</groupId> <artifactId>snakeyaml</artifactId> <version>1.33</version> </dependency>
Java 代碼
package com.scd.tool; import org.yaml.snakeyaml.Yaml; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.LinkedHashMap; import java.util.List; import java.util.Set; /** * @author James */ public class YamlToProperties { public static void main(String[] args) { Yaml yaml = new Yaml(); String filePath = "file/yaml/bootstrap.yml"; try (InputStream inputStream = new FileInputStream(filePath)) { Object object = yaml.load(inputStream); List<String> resultList = travelRootWithResult(object); System.out.println(resultList); } catch (IOException e) { throw new RuntimeException(e); } } private static List<String> travelRootWithResult(Object object) { List<String> resultList = new ArrayList<>(); if (object instanceof LinkedHashMap) { LinkedHashMap map = (LinkedHashMap) object; Set<Object> keySet = map.keySet(); for (Object key : keySet) { List<String> keyList = new ArrayList<>(); keyList.add((String) key); travelTreeNode(map.get(key), keyList, resultList); } } return resultList; } private static void travelTreeNode(Object obj, List<String> keyList, List<String> resultList) { if (obj instanceof LinkedHashMap) { LinkedHashMap linkedHashMap = (LinkedHashMap) obj; linkedHashMap.forEach((key, value) -> { if (value instanceof LinkedHashMap) { keyList.add((String) key); travelTreeNode(value, keyList, resultList); keyList.remove(keyList.size() - 1); } else { StringBuilder result = new StringBuilder(); for (String strKey : keyList) { result.append(strKey).append("."); } result.append(key).append("=").append(value); System.out.println(result); resultList.add(result.toString()); } }); } else { StringBuilder result = new StringBuilder(); result.append(keyList.get(0)).append("=").append(obj); System.out.println(result); resultList.add(result.toString()); } } }
運(yùn)行結(jié)果如下,對比之后發(fā)現(xiàn)沒有出現(xiàn)遺漏的
spring.application.name=xtoon-sys-server spring.cloud.nacos.config.server-addr=localhost:8848 spring.cloud.nacos.config.file-extension=yaml spring.cloud.nacos.config.enabled=true spring.boot.admin.client.url=http://localhost:5001 spring.boot.admin.client.username=admin spring.boot.admin.client.password=admin spring.boot.admin.client.instance.prefer-ip=true management.health.redis.enabled=false management.endpoint.health.show-details=always management.endpoints.web.exposure.include=*
大家使用的時(shí)候只需要改一下filePath
代碼解讀
可以把yml 看成多個(gè)樹,問題就轉(zhuǎn)換成了遍歷樹的問題,我們需要獲取樹的路徑以及子節(jié)點(diǎn)。
樹的路徑是properties的key, 葉子節(jié)點(diǎn)是properties的value
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Spring?Boot?整合?Reactor實(shí)例詳解
這篇文章主要為大家介紹了Spring?Boot?整合?Reactor實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09Java多線程教程之如何利用Future實(shí)現(xiàn)攜帶結(jié)果的任務(wù)
Callable與Future兩功能是Java?5版本中加入的,這篇文章主要給大家介紹了關(guān)于Java多線程教程之如何利用Future實(shí)現(xiàn)攜帶結(jié)果任務(wù)的相關(guān)資料,需要的朋友可以參考下2021-12-12SpringBoot實(shí)現(xiàn)單文件與多文件上傳
本次例子不基于第三方存儲(如七牛云對象存儲、阿里云對象存儲、騰訊云對象存儲等),僅基于本地存儲。本文主要內(nèi)容如下:公共文件存儲代碼;單文件上傳代碼;多文件上傳代碼2021-05-05springboot?集成activemq項(xiàng)目配置方法
這篇文章主要介紹了springboot?集成activemq項(xiàng)目配置方法,e-car項(xiàng)目配置通過引入activemq依賴,本文結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧2024-04-04SpringBoot3實(shí)現(xiàn)Gzip壓縮優(yōu)化的技術(shù)指南
隨著 Web 應(yīng)用的用戶量和數(shù)據(jù)量增加,網(wǎng)絡(luò)帶寬和頁面加載速度逐漸成為瓶頸,為了減少數(shù)據(jù)傳輸量,提高用戶體驗(yàn),我們可以使用 Gzip 壓縮 HTTP 響應(yīng),本文將介紹如何在 Spring Boot 3 中實(shí)現(xiàn) Gzip 壓縮優(yōu)化,需要的朋友可以參考下2025-04-04Spring Boot2配置Swagger2生成API接口文檔詳情
這篇文章主要介紹了Spring Boot2配置Swagger2生成API接口文檔詳情,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-09-09Java利用Redis實(shí)現(xiàn)高并發(fā)計(jì)數(shù)器的示例代碼
這篇文章主要介紹了Java利用Redis實(shí)現(xiàn)高并發(fā)計(jì)數(shù)器的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02