繁体   English   中英

Spring 引导升级到 2.2.6 - 无法将 yaml 属性绑定到对象列表

[英]Spring Boot Upgrade to 2.2.6 - Unable to bind yaml properties to list of objects

我正在将项目升级到 Spring Boot 2.2.6。 以下编译错误将 yaml 属性数据绑定到对象列表 -

** 请注意该项目是在我一直使用的 spring-boot (2.2.1) 的先前版本中编译的**

java.lang.IllegalStateException:无法加载 ApplicationContext 原因:org.springframework.boot.context.properties.ConfigurationPropertiesBindException:创建名为“webUiApplication.States”的bean时出错:无法将属性绑定到“WebUiApplication.States”:前缀=状态,ignoreInvalidFields=false,ignoreUnknownFields=true; 嵌套异常是 org.springframework.boot.context.properties.bind.BindException:无法将“states.defaults”下的属性绑定到 java.util.List

应用程序.yml

   states:
  defaults:
    -
      postal-code: AL
      name: Alabama
    -
      postal-code: AK
      name: Alaska
    -
      postal-code: AZ
      name: Arizona

配置

    @Data 
   @Configuration
   @ConfigurationProperties("states")
   public static class States {

      private List<State> defaults;

      private List<State> docVerify;

      private List<State> registration;

  }

POJO

@Data
@EqualsAndHashCode(onlyExplicitlyIncluded = true)
@ToString(onlyExplicitlyIncluded = true)
public class State implements ListOption {
   public static final Comparator<State> DISPLAY_COMPARATOR = new ListOption.DisplayComparator<>();

   @NonNull private final String postalCode;

   @NonNull private final String name;

@Override
   @EqualsAndHashCode.Include
   public String getValue() {
      return this.postalCode;
   }

   @Override
   @ToString.Include
   public String getLabel() {
      return String.format("%s - %s", postalCode, name);
   }
}

遇到过成员收到类似问题但未能找到解决方案的帖子。 期待您的投入。

重构你的代码:

状态:

@Data

    @Configuration
    @ConfigurationProperties("states")
    @ToString
    @NoArgsConstructor
    public class States {
        private List<State> defaults;
        private List<State> docVerify;
        private List<State> registration;

    }

State:

@Data
@EqualsAndHashCode(onlyExplicitlyIncluded = true)
@ToString(onlyExplicitlyIncluded = true)
@NoArgsConstructor
public class State {
    @NonNull
    private String postalCode;

    @NonNull
    private String name;

    @EqualsAndHashCode.Include
    public String getValue() {
        return this.postalCode;
    }

    @ToString.Include
    public String getLabel() {
        return String.format("%s - %s", postalCode, name);
    }
}

应用程序.yaml

states:
  defaults:
    -
      postal-code: AL
      name: Alabama
    -
      postal-code: AK
      name: Alaska
    -
      postal-code: AZ
      name: Arizona

我们需要有一个空的 object 然后用数据填充它。 这就是我们不需要 args 构造函数的原因。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM