Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java SpringBoot - @Value for en empty list [duplicate]

Small question regarding how to configure an empty list with an @Value via application.properties file please.

I have the following:

  @Value("#{'${my.cool.list}'.split(',')}")
    private List<String> myList;

If I configure the following in my application properties:

my.cool.list=

I was naively expecting to have an empty list.

But instead, I have a list one one element and the one element is the empty string 🤯

How do I declare an empty list via application properties please?

I do not want to use some default mechanism from @Value, or remove the property if possible.

Thank you

like image 704
PatPatPat Avatar asked Oct 15 '25 01:10

PatPatPat


1 Answers

Replace (or trim) the blank spaces first:

@Value("#{'${my.cool.list}'.replace(' ','').split(',')}")
private List<String> myList;

With newer versions, you could also use : as operator for empty Lists:

@Value("#{'${my.cool.list:}'")
private List<String> myList;
like image 174
aran Avatar answered Oct 17 '25 17:10

aran



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!