3

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

PatPanda
  • 3,644
  • 9
  • 58
  • 154

1 Answers1

1

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;
aran
  • 10,978
  • 5
  • 39
  • 69