Yaml file is not processed correctly with @ConfigurationProperties

1.4k Views Asked by At

I'm using Spring Boot 1.4.2 for my small project. My configuration class is as below

@Component
@PropertySource("classpath:global1.yml")
@ConfigurationProperties
public class GlobalProperties {
    private String name;

    private List<Menu> menus = new ArrayList<>();

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public List<Menu> getMenus() {
        return menus;
    }

    public void setMenus(List<Menu> menus) {
        this.menus = menus;
    }

    @Override
    public String toString() {
        return "GlobalProperties{" +
                ", name='" + name + '\'' +
                ", menus=" + menus + '\'' +
                '}';
    }
}

and global1.yml

name: "helloworld"
menus:
    - title: Home
      name: Home
      path: /
    - title: Login
      name: Login
      path: /login

The code was fine if I didn't add the list of menus in the YAML file. But with the file above, I got

Property: target.menus
Value: 
Reason: Failed to convert property value of type 'java.lang.String' to required type 'java.util.List' for property 'menus'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'java.lang.String' to required type 'com.test.Menu' for property 'menus[0]': no matching editors or conversion strategy found

Plus, if I put all these properties in application.yml. Everything works fine. Please explain and help me fix this.

1

There are 1 best solutions below

0
On

I have just figured it out. Based on the doc here: Externalized Configuration, I can't have 2 yaml files and the yaml files can't be loaded via @PropertySource.