Java – How to bind a yml map to a Java map in spring-boot configuration

javaspringspring-bootyaml

I am trying to follow this guide here:

https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-Configuration-Binding

but I am struggling to get it to work.

I want to initialize a HashMap from a map defined in application.yml.

This is my last try at the yml-map definition:

symbols:
    symbolPairs.CombinationsAlpha="CombinationsAlpha"
    symbolPairs.[CombinationsAlpha]=aaabbb, bbbaaa, ccceee, dddggg
    symbolPairs.Combinations="CombinationsInteger"
    symbolPairs.[CombinationsAlpha]=000111, 222666, 999000, 151515

And this is my java class:

@Data
@Configuration
@EnableConfigurationProperties
@ConfigurationProperties(prefix = "symbols")
public class SymbolsConfig {
  private Map<String, List<String>> symbolPairs = new HashMap<>();
}

I want to have "CombinationsAplha" and "CombinationsInteger"injected as keys and the values as a List of Strings.
I am struggling to define the yml correctly.

The @Data annotation from projectlombok generates getters and setters.

Best Answer

Your yml structure isn't correct. Change your yml like this

symbols:
    symbolPairs.[CombinationsAlpha]: aaabbb, bbbaaa, ccceee, dddggg
    symbolPairs.[CombinationsInteger]: 000111, 222666, 999000, 151515

Here is the output

enter image description here