Java Yml格式轉(zhuǎn)換為Properties問題
問題引入
使用在線的yml轉(zhuǎn)換properties, 發(fā)現(xiàn)有屬性內(nèi)容漏了,網(wǎng)站地址https://tooltt.com/yaml2properties/。
于是自己動手寫個轉(zhuǎn)換工具類,自測過多個 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é)果應該如下
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)少了幾個屬性
spring.cloud.nacos.config.enabled=true spring.boot.admin.client.instance.prefer-ip=true management.health.redis.enabled=false
這幾個結(jié)果有些特征,value值是boolean類型的。不知道還有沒有其它類型的數(shù)據(jù)會有遺漏的?
轉(zhuǎn)換代碼
導入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()); } } }
運行結(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=*
大家使用的時候只需要改一下filePath
代碼解讀
可以把yml 看成多個樹,問題就轉(zhuǎn)換成了遍歷樹的問題,我們需要獲取樹的路徑以及子節(jié)點。
樹的路徑是properties的key, 葉子節(jié)點是properties的value
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java多線程教程之如何利用Future實現(xiàn)攜帶結(jié)果的任務
Callable與Future兩功能是Java?5版本中加入的,這篇文章主要給大家介紹了關(guān)于Java多線程教程之如何利用Future實現(xiàn)攜帶結(jié)果任務的相關(guān)資料,需要的朋友可以參考下2021-12-12SpringBoot3實現(xiàn)Gzip壓縮優(yōu)化的技術(shù)指南
隨著 Web 應用的用戶量和數(shù)據(jù)量增加,網(wǎng)絡帶寬和頁面加載速度逐漸成為瓶頸,為了減少數(shù)據(jù)傳輸量,提高用戶體驗,我們可以使用 Gzip 壓縮 HTTP 響應,本文將介紹如何在 Spring Boot 3 中實現(xiàn) Gzip 壓縮優(yōu)化,需要的朋友可以參考下2025-04-04Spring Boot2配置Swagger2生成API接口文檔詳情
這篇文章主要介紹了Spring Boot2配置Swagger2生成API接口文檔詳情,文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下2022-09-09Java利用Redis實現(xiàn)高并發(fā)計數(shù)器的示例代碼
這篇文章主要介紹了Java利用Redis實現(xiàn)高并發(fā)計數(shù)器的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-02-02