Java Yml格式轉(zhuǎn)換為Properties問題
問題引入
使用在線的yml轉(zhuǎn)換properties, 發(fā)現(xiàn)有屬性內(nèi)容漏了,網(wǎng)站地址https://tooltt.com/yaml2properties/。
于是自己動(dòng)手寫個(gè)轉(zhuǎn)換工具類,自測(cè)過多個(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é)果截圖如下

對(duì)比原始文本和轉(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ù)會(huì)有遺漏的?
轉(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é)果如下,對(duì)比之后發(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)文章
Redis Java 集成到 Spring Boot的詳細(xì)過程
本文介紹了如何使用SpringBoot連接Redis,并展示了如何配置Redis服務(wù)地址、創(chuàng)建Controller類以及進(jìn)行基本的Redis操作,如字符串、列表、集合、哈希和有序集合,感興趣的朋友跟隨小編一起看看吧2024-12-12
jdk7 中HashMap的知識(shí)點(diǎn)總結(jié)
HashMap的原理是老生常談了,不作仔細(xì)解說。一句話概括為HashMap是一個(gè)散列表,它存儲(chǔ)的內(nèi)容是鍵值對(duì)(key-value)映射。這篇文章主要總結(jié)了關(guān)于jdk7 中HashMap的知識(shí)點(diǎn),需要的朋友可以參考借鑒,一起來看看吧。2017-01-01
解決 java: 程序包c(diǎn)om.baomidou.mybatisplus.annotation不存在
在使用Java編寫程序時(shí),經(jīng)常會(huì)遇到各種編譯錯(cuò)誤或運(yùn)行時(shí)異常,本文主要介紹了解決java:程序包c(diǎn)om.baomidou.mybatisplus.annotation不存在,具有一定的參考價(jià)值,感興趣的可以了解一下2024-03-03
FilenameUtils.getName?函數(shù)源碼分析
這篇文章主要為大家介紹了FilenameUtils.getName?函數(shù)源碼分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09
idea配置全局變量Jdk、maven倉庫以及maven詳解(全文圖解)
這篇文章主要給大家介紹了關(guān)于idea配置全局變量Jdk、maven倉庫以及maven的相關(guān)資料,在配置JDK和Maven之前,需要確保已經(jīng)正確安裝了JDK和Maven,文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下2024-01-01
Mybatis-Plus中的MetaObjectHandler組件的使用
MetaObjectHandler是Mybatis-Plus中一個(gè)實(shí)用組件,專門用于自動(dòng)處理實(shí)體對(duì)象中的特定字段,如創(chuàng)建時(shí)間、更新時(shí)間、創(chuàng)建人和修改人等,該接口允許開發(fā)者在不修改業(yè)務(wù)代碼的情況下,實(shí)現(xiàn)自動(dòng)填充功能,極大地簡化了代碼的復(fù)雜性,感興趣的可以了解一下2024-10-10
手把手帶你實(shí)現(xiàn)一個(gè)萌芽版的Spring容器
大家好,我是老三,Spring是我們最常用的開源框架,經(jīng)過多年發(fā)展,Spring已經(jīng)發(fā)展成枝繁葉茂的大樹,讓我們難以窺其全貌,這節(jié),我們回歸Spring的本質(zhì),五分鐘手?jǐn)]一個(gè)Spring容器,揭開Spring神秘的面紗2022-03-03

