文件流读取yaml

2023-12-26 15:02:13

本地

    public static void main(String[] args)throws Exception {
        String workingDir = System.getProperty("user.dir");
        String filePath = workingDir+"\\src\\main\\resources\\application.yml";
        FileInputStream in = new FileInputStream(filePath);
        Yaml yaml = new Yaml();


        Map<String, Object> result = yaml.load(in);


        Map<String, Object> serverConfig = (Map<String, Object>) result.get("config");


        String port = serverConfig.get("swagger-version").toString();

        System.out.println("Port: " + port);

    }

服务器


        public static void main(String[] args) throws Exception {
            // 获取 application.yml 文件作为输入流
            InputStream in = Main.class.getResourceAsStream("/BOOT-INF/classes/application.yml");

            Yaml yaml = new Yaml(new Constructor(Map.class));

            // 使用 load 方法加载 YAML 文件
            Map<String, Object> result = yaml.load(in);

            // 从 YAML 结果中获取配置
            Map<String, Object> serverConfig = (Map<String, Object>) result.get("config");
            String port = serverConfig.get("swagger-version").toString();

            System.out.println("Port: " + port);
        }

文章来源:https://blog.csdn.net/weixin_50098749/article/details/135219697
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。