How to bind a property with space to Map key in Spring application.properties file?
Asked Answered
L

4

8

Given an application.properties file:

myProperties.map.key1=value1
myProperties.map.key2=value2
myProperties.map.key with space=value3

And the properties class:

@ConfigurationProperties
public class MyProperties {
    private Map<String, String> map;
    // getters and setters
}

I have tried to escape the spaces like key\ with\ space or even key\u0020with\u0020space but both still ended up as keywithspace in the map. Please point out the correct way to add key with space to map using Spring application.properties, thank you.

Lenity answered 20/11, 2019 at 9:24 Comment(0)
F
10

Happened to run into the same issue, I found the trick to be using [] brackets combined with escaping the whitespace using either \ or \u0020:

myProperties.map.key1=value1
myProperties.map.key2=value2
myProperties.map.[key\ with\ space]=value3

This way the whitespace is preserved in the map key.

I was unable to find documentation for this, purely stumbled upon it by trial and error.

Foremast answered 7/7, 2021 at 14:47 Comment(3)
It works without brackets also, only by escaping space with \Hartford
I found this only works if you do the dot syntax. If you try to do nested property syntax, it will include the brackets in the key. myProperties: map: key1: value1 key2: value2 [key with space]: value3Spirituel
@Spirituel Yes, You are right. did you find any way to achieve the same. If yes, Could you please share.Petuu
S
4

I could do it with different approach.

In application.properties

key\u0020with\u0020space=test

And wherever I want to read, autowired Environment object.

@Autowired
private Environment env

and read the property

String test = env.getProperty("key with space");

In your case if you know the key with spaces you can read the property and then place it in the map. But if you don't know it exactly not sure how it can be handled.

Semple answered 20/11, 2019 at 10:43 Comment(1)
Thank you. But yes, I am looping through the map, so I don't have prior knowledge of the property name.Lenity
J
3

For map keys with non-alphanumeric characters (other than -) in them, surround the key name with [].

In your example: '[myProperties.map.key with space]'=value3

See https://github.com/spring-projects/spring-boot/wiki/Relaxed-Binding-2.0#maps for further details.

Jahn answered 22/9, 2022 at 3:43 Comment(0)
F
1

For me, Spring even removed a period and non-Latin 1 characters (I use YAML, because of this) from the key. Thats why I ended up with a list with the values separated by semicolon:

myMapAsList:
   - key 1;value 1
   - key 2 with äöü.;value 2

You need a wrapper class with the fields key and value:

public class YourWrapper {
    private String key;
    private String value;
    // getter & setter & constructor
}

... plus a converter:

@Component
@ConfigurationPropertiesBinding
public class YourWrapperConverter implements Converter<String, YourWrapper>{

    @Override
    public YourWrapper convert(String source) {
        String[] data = source.split(";");
        return new YourWrapper(data[0], data[1]);
    }

}

In your properties class: private List<YourWrapper> myMapAsList;. Afterwards you may loop through the list and fill your map.

Frowsy answered 3/11, 2020 at 7:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.