[英]Spring Boot Upgrade to 2.2.6 - Unable to bind yaml properties to list of objects
I am in the process to upgrade a project to Spring Boot 2.2.6.我正在将项目升级到 Spring Boot 2.2.6。 Following compilation errors are there wrt to binding yaml properties data to list of objects -
以下编译错误将 yaml 属性数据绑定到对象列表 -
** Please note the project is compiling in previous version of spring-boot (2.2.1) I have been using** ** 请注意该项目是在我一直使用的 spring-boot (2.2.1) 的先前版本中编译的**
java.lang.IllegalStateException: Failed to load ApplicationContext Caused by: org.springframework.boot.context.properties.ConfigurationPropertiesBindException: Error creating bean with name 'webUiApplication.States': Could not bind properties to 'WebUiApplication.States': prefix=states, ignoreInvalidFields=false, ignoreUnknownFields=true;
java.lang.IllegalStateException:无法加载 ApplicationContext 原因:org.springframework.boot.context.properties.ConfigurationPropertiesBindException:创建名为“webUiApplication.States”的bean时出错:无法将属性绑定到“WebUiApplication.States”:前缀=状态,ignoreInvalidFields=false,ignoreUnknownFields=true; nested exception is org.springframework.boot.context.properties.bind.BindException: Failed to bind properties under 'states.defaults' to java.util.List
嵌套异常是 org.springframework.boot.context.properties.bind.BindException:无法将“states.defaults”下的属性绑定到 java.util.List
application.yml应用程序.yml
states:
defaults:
-
postal-code: AL
name: Alabama
-
postal-code: AK
name: Alaska
-
postal-code: AZ
name: Arizona
Configuration配置
@Data
@Configuration
@ConfigurationProperties("states")
public static class States {
private List<State> defaults;
private List<State> docVerify;
private List<State> registration;
}
POJO 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);
}
}
Have come across posts where members have received the similar issue but have not been been able to come to a solution.遇到过成员收到类似问题但未能找到解决方案的帖子。 Looking forward to your inputs.
期待您的投入。
Refactor your code:重构你的代码:
States:状态:
@Data
@Configuration
@ConfigurationProperties("states")
@ToString
@NoArgsConstructor
public class States {
private List<State> defaults;
private List<State> docVerify;
private List<State> registration;
}
State: 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);
}
}
application.yaml应用程序.yaml
states:
defaults:
-
postal-code: AL
name: Alabama
-
postal-code: AK
name: Alaska
-
postal-code: AZ
name: Arizona
We need to have an empty object and then fill it with data.我们需要有一个空的 object 然后用数据填充它。 This is why we need no args constructor.
这就是我们不需要 args 构造函数的原因。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.