Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring application properties ignoring slashes in strings

I am experiencing a weird behaviour when loading external properties to the spring boot (2.0.3.RELEASE) application using application.yaml. I will show a minimal example bellow. I have defined a @ConfigurationProperties class as follows:

@Component
@ConfigurationProperties
class ConfigProperties {
    var testString: String = ""
    var testMap : MutableMap<String, String> = mutableMapOf()
}

I also have an adequate application.yaml file:

testString: "/profile/test"
testMap:
    "/profile/main" : "Welcome/to/profile"

When evaluated, ConfigProperties.testString produces /profile/test as expected but ConfigProperties.testMap produces a map of size one with the only entry's key set to profilemain and value set to Welcome/to/profile. Why does the key ignore a forward slashes (/) but value is resolved properly? I tried escaping the forward slash but that didn't work. I also found that other special characters like colon (:) and backslash (\) also get ignored by the map key.

like image 964
fibheap Avatar asked Sep 06 '25 03:09

fibheap


1 Answers

I know this is old but if someone else is wondering, this is how it works in spring boot 2.

https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-2.0-Migration-Guide#relaxed-binding

For map keys with non-alphanumeric characters (other than -) in them, surround the key name with []. For example:
spring:
  my-example:
    '[foo.baz]': bar
    '[abc xyz]': def
like image 163
michali Avatar answered Sep 07 '25 18:09

michali