'Reading keys with square brackets in YAML files from Spring
I have a .yml file that one of the fields looks like this
a:
b:
"[/folder/**]": "file:./src/"
How can I access this field in Spring using @Value?
I tried the following but all failed with "Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder"
@Value("${a.b./folder/**}") private File path;
@Value("${a.b.[/folder/**]}") private File path;
@Value("${a.b.\\[/folder/**\\]}") private File path;
Solution 1:[1]
Kotlin:
@SpringBootTest
class TestExample {
@Value("\${a.b.[/folder/**]}")
lateinit var file: File
@Test
fun test() {
// ".${File.separator}src" on Windows => .\src
// ".${File.separator}src" on Linux => ./src
assertEquals(".${File.separator}src", file.path)
}
}
It also works with and without the "file:" prefix.
a:
b:
"[/folder/**]": "./src/"
Solution 2:[2]
Your path key is in double quotes.
You must add double quotes in @Value property string like below.
@Value("${a.b.\"[/folder/**]\"}")
private File path;
Solution 3:[3]
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | changuk |
Solution 3 | Numichi |