Injecting a null or empty string as the default value for a property

Is it possible to enter a null or empty string as the default value that will be used in the spring file if the property is not specified? Currently, the best I have is

<constructor-arg index="1" value="@{data.url:&quot;&quot;}"/> 

which allows "" in code

+7
source share
3 answers

This will be the trick if you use annotations in your source:

 @Value("${data.url:#{null}}") private String dataUrl; 
+8
source

Have you tried using SpEL ? Something like this is possible:

 <constructor-arg index="1" value="#{'${data.url}'==null?'':'${data.url}'}"/> 

Update

I just remembered that there is an easier way (as is well described here ). Try:

 <constructor-arg index="1" value="${data.url:''}"/> 
+7
source

Empty Elvis works for me ...

 @Value("${http.proxyHost?:}") public String proxyHost = null; 
+5
source

All Articles