Escaping a dot in a Map key in Yaml in Spring Boot
A slight revision of @fivetenwill's answer, which works for me on Spring Boot 1.4.3.RELEASE:
foo:
"[bar.com]":
a: b
"[baz.com]":
a: c
You need the brackets to be inside quotes, otherwise the YAML parser basically discards them before they get to Spring, and they don't make it into the property name.
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:
https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html
So, your YAML is converted to:
foo.bar.com.a=b
foo.baz.com.a=c
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.
@Configuration
public class Config {
private Map<String, Object> foo;
@Bean
public Map<String, Object> setup() {
foo = yamlFactory().getObject();
System.out.println(foo); //Prints {foo={bar.com={a=b}, baz.com={a=c}}}
return foo;
}
@Bean
public YamlMapFactoryBean yamlFactory() {
YamlMapFactoryBean factory = new YamlMapFactoryBean();
factory.setResources(resource());
return factory;
}
public Resource resource() {
return new ClassPathResource("a.yaml"); //a.yaml contains your yaml config in question
}
}
This should work:
foo:
"[bar.com]":
a: b
"[baz.com]":
a: c
Inspired from Spring Boot Configuration Binding Wiki