Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 811
user1589188 Avatar asked Jan 26 '23 14:01

user1589188


1 Answers

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.

like image 84
Saberos Avatar answered Jan 29 '23 23:01

Saberos