SpringBoot写配置文件报错“The elements [xxx,xxx] were left unbound.“

Posted 二木成林

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringBoot写配置文件报错“The elements [xxx,xxx] were left unbound.“相关的知识,希望对你有一定的参考价值。

异常

在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

 

以上是关于SpringBoot写配置文件报错“The elements [xxx,xxx] were left unbound.“的主要内容,如果未能解决你的问题,请参考以下文章

SpringBoot集成RocketMQ报错:Bad annotation definition in @ExtRocketMQTemplateConfiguration...

idea springboot连接mysql 配置datasource报错?

SpringBoot读取配置文件信息显示报错

13.7 SpringBoot集成日志系统logback的几个问题

启动springboot项目连接nacos控制台报错com.alibaba.nacos.api.exception.NacosException: dataId invalid

springboot启动报错可以测试吗