0

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. 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. 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. It keeps throwing an error for me or the map object is null (or not being populated).

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:

@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

how to convert yml file to java pojo

Am I missing something obvious? Thanks!

rj2700
  • 1,770
  • 6
  • 28
  • 55

2 Answers2

0

You can try to create a POJO to represent the yml structure you are trying to read from.

For example:

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

    private String now;

    private String later;
}
bruno.zambiazi
  • 1,422
  • 14
  • 19
  • Thanks for this response but I'm trying to create a map object from this. Something I didn't mention in the original question was that I don't know the children (other than there will be no duplicate keys), that said, I rather create a POJO that can create map objects dynamically rather than hard code those values. – rj2700 Nov 02 '17 at 19:39
0

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.

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).

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:

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

The POJO would look like this:

@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.

rj2700
  • 1,770
  • 6
  • 28
  • 55