Spring-boot: Regression with property binding to List<String>

Created on 30 May 2018  路  4Comments  路  Source: spring-projects/spring-boot

With 1.5.x I used to have a property binding like this; a comma-separated String to List<String>.

@ConfigurationProperties(prefix = "foo.bar")
public class MyAppProperties {
    private final Kafka kafka = new Kafka();

    public Kafka getKafka() {
        return this.kafka;
    }

    public static class Kafka {
        private List<String> topics;
        public void setTopics(List<String> topics) {
            this.topics = topics;
        }
...
foo.bar:
    kafka:
      topics: test,failures

With Boot 2.0.2 this now fails with

Failed to bind properties under 'foo.bar.kafka.topics' to java.util.List<java.lang.String>:

    Reason: Unable to get value for property topics

Replacing List<String> in the setter with String[] fixes this.

public void setTopics(String[] topics) {
  this.topics = Lists.newArrayList(topics);
}
bug

All 4 comments

According to the documentation, binding requires a getter. Do I understand you don't have a getter there (i.e. it's not an oversight)?

The getter is there - below the ... in the initial snippet 馃槈 For the sake of completeness here it is:

public List<String> getTopics() {
  return Collections.unmodifiableList(this.topics);
}

It fails when the CollectionBinder tries to get the existing value for the collection here. In this case, it's a NPE. I think we should guard against this.

@mbhave spot on 馃憤 If topics is initialized with an empty collection then I don't need the String[] workaround.

Was this page helpful?
0 / 5 - 0 ratings