Special characters in a key in Spring Boot YAML file

To use some special characters in a key of a property in a YAML file (like application.yml) processed by Spring Boot 2.0 (or later) you need to use specific syntax. Key element containing “/” or “@” or “+” (and possibly some other special characters) needs to be surrounded by square brackets. Example:

foo.bar:
  "[http://something]": value

This is needed to define property named "foo.bar.http://something" in Spring Boot 2.0 or later. According to Spring Boot issue #13404 (Allow map binding to work with unescaped characters) it was not needed in Spring Boot 1.5 (and most likely in earlier versions).

If you don’t use square brackets in such a case when using Spring Boot 2.0 or later the actual property name will be different causing things like value injection (@Value annotation) to not work.

Another Spring Boot issue, #14017 (Property binder does not allow special characters in map keys), explains that with Spring Boot 2.0 (or later) special characters are dropped silently when square brackets are not used.

It seems this feature was not document well for some time – see Spring Boot issue #13506 (Document when and how to use bracket notation when binding to a map).

P.S.
I’m not claiming it is a good idea to use such characters as “/”, “@” or “+” in property keys with Spring Boot. I recommend to avoid such design decisions as it may make it really hard to override properties using for example environment variables.

About krzysztoftomaszewski

I've got M.Sc. in software engineering. I graduated in 2005 at Institute of Computer Science, Warsaw University of Technology, Faculty of Electronics and Information Technology. I'm working on computer software design and engineering continuously since 2004.
This entry was posted in Java, Spring and tagged , . Bookmark the permalink.

3 Responses to Special characters in a key in Spring Boot YAML file

  1. Sabrina Kloppig says:

    Saved my day. Thank you!

  2. Rajesh Kumar Gajjala says:

    It’s saved my day too in a big way

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s