问题引入
使用在线的yml转换properties, 发现有属性内容漏了,网站地址https://tooltt.com/yaml2properties/。
于是自己动手写个转换工具类,自测过多个 yml 文件,目前没发现遗漏的。
需要转换的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: "*"
在线转换网站转换的结果
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=*
正确的转换结果应该如下
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=*
在线网站转换结果截图如下
对比原始文本和转换结果,发现少了几个属性
spring.cloud.nacos.config.enabled=true
spring.boot.admin.client.instance.prefer-ip=true
management.health.redis.enabled=false
这几个结果有些特征,value值是boolean类型的。不知道还有没有其它类型的数据会有遗漏的?
转换代码
导入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());
}
}
}
运行结果如下,对比之后发现没有出现遗漏的
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文章来源:https://www.toymoban.com/news/detail-661873.html
代码解读
可以把yml 看成多个树,问题就转换成了遍历树的问题,我们需要获取树的路径以及子节点。树的路径是properties的key, 叶子节点是properties的value文章来源地址https://www.toymoban.com/news/detail-661873.html
到了这里,关于Java Yml格式转换为Properties的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!