read list of objects from .yml with Spring-Boot 2.1

547 views Asked by At

I need to create @ConfigurationProperties that reads .yml file containing list of complex objects.

it seems like Spring-Boot is having some issues with this so I go step-by-step and try to load list of strings first:

.yml:

qwer: asdf
strings:
    - Apple
    - Orange
    - Strawberry
    - Mango

config class:

@Component
@PropertySource(value = {"tsp_client.yaml", "file:tsp_client.yaml"}, ignoreResourceNotFound = true)
@ConfigurationProperties
public class TSPClientConfig {
    public String qwer;

    public List<String> strings;

    public String getQwer() {
        return qwer;
    }

    public void setQwer(String qwer) {
        this.qwer = qwer;
    }

    public List<String> getStrings() {
        return strings;
    }

    public void setStrings(List<String> strings) {
        this.strings = strings;
    }
}

and with this I'm still getting list of size 0. qewr property maps fine through. indentation on strings should be ok as I copied it from here.

So, can you tell me whether is spring having some issues with this or I'm doing something wrong here? ultimately I need to have complex objects in list.

Spring-Boot: 2.1.2.RELEASE

1

There are 1 answers

1
jonathan Heindl On

apparently jackson also has a yaml reader : https://dzone.com/articles/read-yaml-in-java-with-jackson

and in case it doesnt have a List setter you can just do

setStrings(String[] ar){
this.strings=Arrays.asList(ar);
}