Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have a following yml config:

foo:
  bar.com:
    a: b
  baz.com:
    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. 'bar.com' or "bar.com") but to no avail - still the same problem. Is there a way around this?

like image 827
theadam Avatar asked Dec 03 '15 16:12

theadam


People also ask

How do I escape dot in YAML?

YAML uses similar slash style escape sequences as C. In YAML, \n is used to represent a new line, \t is used to represent a tab, and \\ is used to represent the slash. In addition, since whitespace is folded, YAML uses bash style "\ " to escape additional spaces that are part of the content and should not be folded.

Can YAML keys contain dots?

The dot doesn't signify anything special in YAML—the behavior you're seeing is happening in Spring after the YAML is deserialized.

How do I get YAML properties in spring boot?

To access the YAML properties, we'll create an object of the YAMLConfig class, and access the properties using that object. In the properties file, we'll set the spring. profiles. active environment variable to prod.

What does the properties keyword in YAML use for?

Let's define a list in such files: YAML as part of its specification supports the list. . properties File: This file extension is used for the configuration application. These are used as the Property Resource Bundles files in technologies like Java, etc.


3 Answers

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.

like image 117
pioto Avatar answered Oct 08 '22 03:10

pioto


This should work:

foo:
  "[bar.com]":
    a: b
  "[baz.com]":
    a: c

Inspired from Spring Boot Configuration Binding Wiki

like image 44
fivetenwill Avatar answered Oct 08 '22 04:10

fivetenwill


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
    }

}
like image 43
Mohit Avatar answered Oct 08 '22 02:10

Mohit