异常

在SpringBoot的配置文件application.yaml中写配置报错:

2021-06-22 21:01:01.962  WARN 28928 --- [           main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.boot.context.properties.ConfigurationPropertiesBindException: Error creating bean with name 'person': Could not bind properties to 'Person' : prefix=person, ignoreInvalidFields=false, ignoreUnknownFields=true; nested exception is org.springframework.boot.context.properties.bind.BindException: Failed to bind properties under 'person.allpets.sick' to java.util.List<com.demo.springboot.bean.Pet>
2021-06-22 21:01:01.966  INFO 28928 --- [           main] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2021-06-22 21:01:01.985  WARN 28928 --- [           main] o.a.c.loader.WebappClassLoaderBase       : The web application [ROOT] appears to have started a thread named [Abandoned connection cleanup thread] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread:
 java.lang.Object.wait(Native Method)
 java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:143)
 com.mysql.jdbc.AbandonedConnectionCleanupThread.run(AbandonedConnectionCleanupThread.java:43)
2021-06-22 21:01:01.994  INFO 28928 --- [           main] ConditionEvaluationReportLoggingListener : 

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2021-06-22 21:01:02.003 ERROR 28928 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

Binding to target [Bindable@1b0565c type = java.util.List<com.demo.springboot.bean.Pet>, value = 'none', annotations = array<Annotation>[[empty]]] failed:

    Property: person.allpets.sick[0].name
    Value: 张三
    Origin: class path resource [application.yaml]:39:15
    Reason: The elements [person.allpets.sick[0].name,person.allpets.sick[0].weight] were left unbound.
    Property: person.allpets.sick[0].weight
    Value: 17
    Origin: class path resource [application.yaml]:39:27
    Reason: The elements [person.allpets.sick[0].name,person.allpets.sick[0].weight] were left unbound.

Action:

Update your application's configuration

其中application.yaml

person:
  user-name: zhangsan
  boss: false
  birth: 1997/12/12
  age: 24
  pet:
    name: 小花
    weight: 15
  interests:
    - 阅读
    - 游戏
    - 音乐
    - 学习
  animal:
    - 熊猫
    - 脑斧
    - 小松许
    - 猴子
  score:
    chinese:
      first: 99
      second: 88
      third: 59
    math:
      - 96
      - 78
      - 66
  salarys:
    - 4000
    - 6000
    - 8000
  allPets:
    sick:
      [{name: 张三, weight: 17}]

Person.java

@Data
@ToString
@Component
@ConfigurationProperties(prefix = "person")
public class Person {
    private String userName;
    private Boolean boss;
    private Date birth;
    private Integer age;
    private Pet pet;
    private String[] interests;
    private List<String> animal;
    private Map<String, Object> score;
    private Set<Double> salarys;
    private Map<String, List<Pet>> allPets;
}

Pet.java

@Data
public class Pet {
    private String name;
    private Double weight;

    public Pet(String name) {
        this.name = name;
    }
}

原因

我在Pet类中使用了lombok来生成get、set方法,但是我自定义了一个只有一个参数的构造器,而没有无参构造器,所以springboot无法创建对象。

解决

因此我们需要手动添加无参构造器,或者使用lombok的@NoArgsConstructor注解。

或者

参考链接:https://stackoverflow.com/questions/53167040/springboot-2-the-elements-were-left-unbound

 

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐