简体   繁体   English

读取yaml配置

[英]Reading yaml config

I've been trying to wrap my head around this for hours but it doesn't make much sense what I'm doing wrong. 数小时以来,我一直在努力思考,但是我做错了事没有多大意义。 I am trying to create a Map object in Java from a .yml file. 我正在尝试从.yml文件在Java中创建Map对象。 Reason being for a map, I don't know what/how many children will appear under "present", so I rather have a dynamic way of creating a map object out of that.. 之所以要使用地图,是因为我不知道“呈现”下将出现多少个孩子,所以我宁愿有一种动态的方式来创建地图对象。

Below is my .yml file. 以下是我的.yml文件。 I would like the key-value pair under "present": 我想要“存在”下的键值对:

present:
    now: LOCAL TESTING
    later: testing

Below is my config class (everything commented out is what I've tried - in different combinations): 下面是我的配置类(所有注释掉的都是我尝试过的内容-以不同的组合):

//@Data
@Component
@EnableConfigurationProperties
@ConfigurationProperties(prefix = "present")
//@ConfigurationProperties
public class stat {

    //@Getter
    //@Data
    @Value("${present}")
    private Map<String, String> present;
    //private Map<String, String> present = new HashMap<String, String>();

}

I tried looking at other SO posts and I feel like I understand it but my Spring Boot (v1.5.8) application isn't seeing that value. 我尝试查看其他SO帖子,我觉得自己理解了,但是我的Spring Boot(v1.5.8)应用程序没有看到该值。 It keeps throwing an error for me or the map object is null (or not being populated). 它一直为我抛出错误,或者地图对象为null(或未填充)。

I know that I can read values from this .yml file because if I try to obtain a single value with a code snippet below, it works: 我知道我可以从此.yml文件中读取值,因为如果我尝试使用下面的代码段获取单个值,则它可以正常工作:

@Data
@Value("${present.now}")
private String status; // String value "LOCAL TESTING"

Here are the other links that I've tried: 这是我尝试过的其他链接:

Spring Boot yaml configuration for a list of strings Spring Boot Yaml配置以获取字符串列表

how to convert yml file to java pojo 如何将yml文件转换为java pojo

Am I missing something obvious? 我是否缺少明显的东西? Thanks! 谢谢!

You can try to create a POJO to represent the yml structure you are trying to read from. 您可以尝试创建一个POJO来表示您要读取的yml结构。

For example: 例如:

@Configuration
@ConfigurationProperties(prefix = "present")
@Data
public class Present {

    private String now;

    private String later;
}

So I figured it out (for those who have this problem later): 所以我弄清楚了(对于以后有这个问题的人):

@Value isn't needed and the prefix param in the @ConfigurationProperties isn't needed. 不需要@Value,也不需要@ConfigurationProperties中的前缀param。

Then you need to have a getter method for the fields that you want - I thought the Lombok library had autogenerated these but I was wrong (probably need to read more about it later - @Setter and @Data won't work properly). 然后,您需要为所需的字段提供一种getter方法-我认为Lombok库已经自动生成了这些字段,但是我错了(可能以后需要阅读更多内容-@Setter和@Data无法正常工作)。

So it should look something like this: 所以它应该看起来像这样:

@Component
@EnableConfigurationProperties
@ConfigurationProperties
public class stat {

    private Map<String, String[]> present = new HashMap<String, String[]>(); 

    public Map<String, String[]> getPresent() {
        return present;
    }
}

Now let's give a more complex example (nested maps). 现在,让我们给出一个更复杂的示例(嵌套地图)。 Say that my .yml file looks like this: 假设我的.yml文件如下所示:

parent: 
    present:
        foo: dey, tok
        bar: ar, jerbs
    later:
        foo: day, dok
        mar: r, darbs

The POJO would look like this: POJO如下所示:

@Component
@EnableConfigurationProperties
@ConfigurationProperties
public class stat {

    private Map<String, Map<String, String[]>> parent = new HashMap<String, Map<String, String[]>>();

    public Map<String, Map<String, String[]>> getParent() {
        return parent;
    }
}

Another key thing to note is that the field that you are obtaining a value from must match the variable name - it may not matter if you use prefix but it still didn't work for me. 另一个要注意的关键是,您要从中获取值的字段必须与变量名匹配-如果使用前缀可能并不重要,但对我而言仍然无效。 Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM