mymap that works with my.prefix.mymap={a=3, b" /> mymap that works with my.prefix.mymap={a=3, b" /> mymap that works with my.prefix.mymap={a=3, b"/>

Mixing @Value of map and fallback value in Spring

71 Views Asked by At

Trying to get an optional map in a config, I tried

@Value("#{${my.prefix.mymap} }") Map<String, Integer> mymap

that works with my.prefix.mymap={a=3, b=4} in the property file and fails if not (defined in a property file)

Sounded neat so far.

But

@Value("#{${my.prefix.mymap : { a : 1, b : 2 } } }") Map<String, Integer> mymap

or

@Value("#{${my.prefix.mymap ?: { a : 1, b : 2 } } }") Map<String, Integer> mymap

give 1 and 2 as values, whatever is defined in the property file.

I wonder if I missed a magic syntax, or that I was hoping too much.
Is that a bug (from me or the framework) or just a frustrating lack of feature ?

Spring is 6.0.11 here

[Edit 1]

Tried, after receiving first comment : @Value("#{${my.prefix.mymap} ?: { a : 1, b : 2 } }") Map<String, Integer> mymap but got Could not resolve placeholder.

Also tried, then : @Value("#{${my.prefix.mymap:null} ?: { a : 1, b : 2 } }") but got the same problem when defined in the property file : I get the default value.

[Edit 2]

Tried @Value("${my.prefix.mymap : #{ {a:1, b:2} } }") Optional<Map<String, Integer>>
and got : No converter found capable of converting from type [java.util.Collections$UnmodifiableMap<?, ?>] to type [java.lang.String]
when my.prefix.mymap={a:2,b:3} is in the properties file

[Edit 3]

Got same answer when my.prefix.mymap=#{{a:2,b:3}} is in the properties file, too.

0

There are 0 best solutions below