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

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值