我正在将一个项目升级到SpringBoot2.2.6。以下是wrt将yaml属性数据绑定到对象列表的编译错误-
**请注意,该项目正在编译以前版本的spring (2.2.1),我一直在使用**。
org.springframework.boot.context.properties.ConfigurationPropertiesBindException:错误创建名为“webUiApplication.state”的bean :无法将属性绑定到“WebUiApplication.States”:prefix=states、ignoreInvalidFields=false、ignoreUnknownFields=true;嵌套异常是org.springframework.boot.context.properties.bind.BindException:未能将属性绑定到java.util.List下
application.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;
}波霍
@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);
}
}曾遇到议员收到类似问题但未能找到解决办法的职位。期待您的投入。
发布于 2020-05-05 05:05:16
重构代码:
各国:
@Data
@Configuration
@ConfigurationProperties("states")
@ToString
@NoArgsConstructor
public class States {
private List<State> defaults;
private List<State> docVerify;
private List<State> registration;
}状态:
@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
states:
defaults:
-
postal-code: AL
name: Alabama
-
postal-code: AK
name: Alaska
-
postal-code: AZ
name: Arizona我们需要一个空对象,然后用数据填充它。这就是为什么我们不需要args构造函数。
https://stackoverflow.com/questions/61552600
复制相似问题