theadam theadam - 8 months ago 171
YAML Question

Escaping a dot in a Map key in Yaml in Spring Boot

I have a following yml config:

a: b
a: c

With a following class Spring tries to inject map with keys 'bar' and 'baz', treating dots as separator:

public class JavaBean {
private Map<String, AnotherBean> foo;

I have tried quoting the key (i.e. '' or "") but to no avail - still the same problem. Is there a way around this?


This is not possible if you want automatic mapping of yaml keys to Java bean attributes. Reason being, Spring first convert YAML into properties format. See section 24.6.1 of link below:

So, your YAML is converted to:

Above keys are parsed as standard properties.

As a work around, you can use Spring's YamlMapFactoryBean to create a Yaml Map as it is. Then, you can use that map to create Java beans by your own.

public class Config {

    private Map<String, Object> foo;

    public Map<String, Object> setup() {
        foo = yamlFactory().getObject();
        System.out.println(foo); //Prints {foo={{a=b},{a=c}}}
        return foo;

    public YamlMapFactoryBean yamlFactory() {
        YamlMapFactoryBean factory = new YamlMapFactoryBean();
        return factory;

    public Resource resource() {
        return new ClassPathResource("a.yaml"); //a.yaml contains your yaml config in question