Failed to load properties from application.yml

Cerin :

I want to load server properties from application.yml to Configuration class . I have seen that many have already asked same question, but none worked for me :( Please help me to figure what i am missing

@Configuration
  @ConfigurationProperties("demo")
  public class Democonfig {
    private List<Archive> archive = new ArrayList<>();
    public Democonfig(List<Archive> archive) {
        this.archive = archive;
    }
    // Getter and setter
   public static class Archive {
        private String host;
        private String database;
        private String port;

        public Archive(String host, String database, String port) {
            this.host = host;
            this.database = database;
            this.port = port;
        }
           // Getters and setters
   }
}

application.yml
demo:
  archive:
  -
    host: "localhost"
    database: "archive1"
    port: "27017"
  -
    host: "localhost"
    database: "archive2"
    port: "27017" 

its showing exception

Binding to target [Bindable@129425f type = java.util.List<com.example.demo.config.Democonfig$Archive>, value = 'provided', annotations = array<Annotation>[[empty]]] failed:

    Property: demo.archive[0].database
    Value: archive1
    Origin: class path resource [application.yml]:5:15
    Reason: The elements [demo.archive[0].database,demo.archive[0].host,demo.archive[0].port,demo.archive[1].database,demo.archive[1].host,demo.archive[1].port] were left unbound.
    Property: demo.archive[0].host
    Value: localhost
    Origin: class path resource [application.yml]:4:11
    Reason: The elements [demo.archive[0].database,demo.archive[0].host,demo.archive[0].port,demo.archive[1].database,demo.archive[1].host,demo.archive[1].port] were left unbound.
    Property: demo.archive[0].port
    Value: 27017

Click here! for full source code and project uploaded in git

Deadpool :

Just missing the No arg constructor in nested static class

public static class Archive {
    private String host;
    private String database;
    private String port;

    public Archive() {
        // TODO Auto-generated constructor stub
    }

    public Archive(String host, String database, String port) {
        System.out.println("constri=uu Archive");
        this.host = host;
        this.database = database;
        this.port = port;
    }
    public String getHost() {
        return host;
    }
    public void setHost(String host) {
        this.host = host;
    }
    public String getDatabase() {
        return database;
    }
    public void setDatabase(String database) {
        this.database = database;
    }
    public String getPort() {
        return port;
    }
    public void setPort(String port) {
        this.port = port;
    }
    @Override
    public String toString() {
        return "Archive [host=" + host + ", database=" + database + ", port=" + port + "]";
    }

}

There is actually no need to provide constructor with argument.Only setters and getters are fine.I would also encourage u to check out and use lombok, it would make your code quire concise .

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=112660&siteId=1