亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

java實現Yaml轉Json示例詳解

 更新時間:2023年02月13日 10:01:06   作者:Allein  
這篇文章主要為大家介紹了java實現Yaml轉Json示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪

緣起

年前,因為項目需要進行配置的優(yōu)化和架構的升級,領導給我來了個任務,讓我去進行技術調研

需要將配置中心的yaml配置文件里面的配置轉為Json的形式,以便后面可以通過寫配置文件的方式,讓數據源變的可拔插

調研過程

本著有輪子就直接用,絕不自己造輪子的態(tài)度,我成功找到了 snakeyaml 這個依賴

<dependency>
  <groupId>org.yaml</groupId>
  <artifactId>snakeyaml</artifactId>
  <version>1.30</version>
</dependency>

1.0 入口點

Yaml yaml = new Yaml();

注意??:此實現并不是一個線程安全的

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 文檔轉為 Java 對象
        Map<String, Object> load = yaml.load(resourceAsStream);
        JSONObject jsonObject = JSONObject.parseObject(load);
        System.out.println(jsonObject.toString());
    }

結果

{"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)建對應的實體類

@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 文檔轉為 Cloud 對象
        Cloud cloud = yaml.load(resourceAsStream);
        JSONObject jsonObject = JSONObject.parseObject(JSONObject.toJSONString(cloud));
        System.out.println(jsonObject.toString());
    }

結果

{"cloud":{"discovery":"consul","config":"consul"},"extension":[{"data":"application-oracle-public.yml","refresh":"true"}]}

1.3 實戰(zhàn)

1.3.1 從本地讀配置文件

在項目中,假如領導只需要 spring.cloud.nacos 這一塊的配置內容,但我們把整個配置文件的內容全部取出來了,那肯定是不符合領導需求的

于是就有了接下來的一些代碼,一些關鍵的注釋也標注在了代碼中

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 文檔轉為 Map
        Map<String, Object> load = yaml.load(resourceAsStream);
        HashMap<String, Object> objectObjectHashMap = new HashMap<>();
        // 通過 key 取到對應的 value 然后放到新的 Map 中
        objectObjectHashMap.put("spring.cloud.nacos", load.get("spring.cloud.nacos"));
        JSONObject jsonObject = JSONObject.parseObject(JSONObject.toJSONString(objectObjectHashMap));
        System.out.println(jsonObject.toString());
    }

結果

{"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)內部類的方式去保證線程的安全

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() {
 &nbsp; &nbsp;String key = "spring.cloud.nacos";
 &nbsp; &nbsp;YamlToJsonUtil yamlToJsonUtil = YamlToJsonUtil.getInstance();
 &nbsp; &nbsp;JSONObject jsonObject = yamlToJsonUtil.YamlToJson(key);
 &nbsp; &nbsp;System.out.println(jsonObject);
}

到了這一步領導的需求基本已經完成了一大半,接下來只需要再從配置中心中將這些配置取出來就可以摸魚下班了

那么問題來了:該如何從配置中心中找到那一份配置文件呢?

就在這時,我想起來了 Spring 里面的 org.springframework.core.env.PropertySource 這個類。 org.springframework.core.env.PropertySource 這一個類顧名思義就是屬性源的意思,再具體一點就是對獲取鍵值對資源的抽象類。我們可以從org.springframework.core.env.AbstractEnvironment#getPropertySources 這個方法中得到配置中心里面的配置以及環(huán)境配置,再通過過濾即可得到我們想到的數據

(ps:上面提到的這個類以及對應的方法大家可以自行百度,因為篇幅等原因就不在這里詳解了)

1.3.2 從配置中心讀配置文件

接下來直接看代碼:

public Map<String, Object> getPropertyMap(Environment environment) {
    // 得到當前環(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);
        }
    }
    // 將集合中多個 LinkedHashMap 合并為一個 Map
    return list.stream()
            .flatMap(map -> map.entrySet().stream())
            .collect(Collectors.toMap(
                    Map.Entry::getKey,
                    Map.Entry::getValue,
                    (value1, value2) -> value1));
}

經過測試,上面的代碼可以將配置中心中的所有配置都存儲在一個 Map<String, Object> 中。

但是由于 yaml.load() 并沒有入參為 Map<String, Object> 的重載方法,所以我們還得引入這個依賴

<dependency>
    <groupId>pl.jalokim.propertiestojson</groupId>
    <artifactId>java-properties-to-json</artifactId>
    <version>5.1.0</version>
</dependency>

這個依賴里面的方法可以將 Map 轉為 properties,再轉為 json,剛好滿足我們的需求。

Map<String, Object> propertyMap = getPropertyMap(environment);
// 將 Map 轉為 Json
String json = new PropertiesToJsonConverter().convertFromValuesAsObjectMap(propertyMap);

直接上整個從配置中心讀yaml配置文件里面的配置轉為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 轉為 Json
        String json = new PropertiesToJsonConverter().convertFromValuesAsObjectMap(propertyMap);
        Yaml yaml = instance.getYaml();
        Map<String, Object> load = null;
        Map<String, Object> result = new HashMap<>();
        // 解析 String 中唯一的 YAML 文檔并轉為 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) {
        // 得到當前環(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);
            }
        }
        // 將集合中多個 LinkedHashMap 合并為一個 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;
    /**
     * 實現ApplicationContextAware接口的回調方法,設置上下文環(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的實例對象
     */
    public static Object getBean(String name) throws BeansException {
        return getApplicationContext().getBean(name);
    }
    public static <T> T getBean(Class<T> className) {
        return getApplicationContext().getBean(className);
    }
    /**
     * 通過接口類型,返回所有實現了這個接口的實現類
     *
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T> Map<String, T> getBeansOfType(Class<T> clazz) {
        return getApplicationContext().getBeansOfType(clazz);
    }
}

測試:

@Test
public void YamlToJsonEnvironment() {
    YamlToJsonForCloudUtil yamlToJsonUtil = YamlToJsonForCloudUtil.getInstance();
    JSONObject dom = yamlToJsonUtil.YamlToJson("dom");
    System.out.println(JSONObject.toJSONString(dom));
}

以上就是java實現Yaml轉Json示例詳解的詳細內容,更多關于java實現Yaml轉Json的資料請關注腳本之家其它相關文章!

相關文章

  • javax.validation.constraints如何校驗參數合法性

    javax.validation.constraints如何校驗參數合法性

    本文將深入探討javax.validation.constraints的基本用法和高級應用,幫助讀者更好地理解和運用這個強大的校驗框架,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-07-07
  • Spring中Properties的配置方式

    Spring中Properties的配置方式

    這篇文章主要介紹了Spring中Properties的配置方式,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-02-02
  • 詳解Java?缺失的特性擴展方法

    詳解Java?缺失的特性擴展方法

    這篇文章主要為大家介紹了Java?缺失的特性擴展方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-03-03
  • java中實現token過期失效超時

    java中實現token過期失效超時

    在Java應用程序中,為了確保安全性和保護用戶數據,一種常見的做法是使用Token進行身份驗證和授權,Token是由服務器生成的具有一定時效的令牌,用于識別和驗證用戶身份,當Token失效后,用戶將無法再進行相關操作,從而提高系統(tǒng)的安全性
    2023-10-10
  • Mybatis關于動態(tài)排序 #{} ${}問題

    Mybatis關于動態(tài)排序 #{} ${}問題

    這篇文章主要介紹了Mybatis關于動態(tài)排序 #{} ${}問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-10-10
  • java單機接口限流處理方案詳解

    java單機接口限流處理方案詳解

    這篇文章主要為大家詳細介紹了java單機接口限流處理方案,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-11-11
  • 使用java自帶des加密算法實現文件加密和字符串加密

    使用java自帶des加密算法實現文件加密和字符串加密

    這篇文章主要介紹了使用java自帶des加密算法實現文件加密和字符串加密的示例,需要的朋友可以參考下
    2014-03-03
  • Mybatis中特殊SQL的執(zhí)行

    Mybatis中特殊SQL的執(zhí)行

    這篇文章主要介紹了Mybatis中特殊SQL的執(zhí)行,介紹內容包括模糊查詢、批量刪除、動態(tài)設置表名、添加功能獲取自增的主鍵等相關資料,需要的小伙伴可以參考一下
    2022-04-04
  • springboot使用redis緩存亂碼(key或者value亂碼)的解決

    springboot使用redis緩存亂碼(key或者value亂碼)的解決

    在通過springboot緩存數據的時候,發(fā)現key是一堆很不友好的東西,本文主要介紹了springboot使用redis緩存亂碼(key或者value亂碼)的解決,感興趣的可以了解一下
    2023-11-11
  • Idea之沒有網絡的情況下創(chuàng)建SpringBoot項目的方法實現

    Idea之沒有網絡的情況下創(chuàng)建SpringBoot項目的方法實現

    本文主要介紹了Idea之沒有網絡的情況下創(chuàng)建SpringBoot項目的方法實現,文中通過圖文介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2023-09-09

最新評論