我在我的spring boot yaml文件中有以下结构:
countryConfiguration:
NL:
address:
postcodeKeyboardType: ALPHANUMERIC
postcodeExample: 1111 AA
cityExample: Amsterdam
ES:
address:
postcodeKeyboardType: NUMERIC
postcodeExample: 11111
cityExample: Madrid我想创建一个配置属性类来访问这些值。我有这样的东西:
@Configuration
@ConfigurationProperties
@Validated
public class CountryConfigurationProperties {
@NotNull
private Map<String, Configuration> countryConfiguration;
public Map<String, Configuration> getCountryConfiguration() {
return countryConfiguration;
}
public void setCountryConfiguration(Map<String, Configuration>
countryConfiguration) {
this.countryConfiguration = countryConfiguration;
}
public static class Configuration {
private Object address;
public Object getAddress() {
return address;
}
public void setAddress(Object address) {
this.address = address;
}
}
}但它不起作用,我得到这样的结论:绑定到目标org.springframework.boot.context.properties.bind.BindException:失败,无法将'‘下的属性绑定到io.bux.onboarding.application.config.CountryConfigurationProperties$$EnhancerBySpringCGLIB$$1d9a5856失败:
Property: .countryConfiguration
Value: null
Reason: must not be null如果我删除静态内部类配置,并放入Object,它就会工作……
发布于 2018-08-24 21:52:37
我注意到address字段的类型为Object。我希望它是Address类型,并且应该有一个表示Address对象的内部类。
在下面的代码片段中,我添加了一个Address类来匹配您正在使用的yml配置。我对此进行了测试,它成功启动并相应地映射了属性。
@Validated
@Component
@ConfigurationProperties
public class CountryConfigurationProperties {
@NotNull
private Map<String, Configuration> countryConfiguration;
public Map<String, Configuration> getCountryConfiguration() {
return countryConfiguration;
}
public void setCountryConfiguration(Map<String, Configuration> countryConfiguration) {
this.countryConfiguration = countryConfiguration;
}
public static class Configuration {
private Address address;
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
}
public static class Address {
private String postcodeKeyboardType;
private String postcodeExample;
private String cityExample;
public String getPostcodeKeyboardType() {
return postcodeKeyboardType;
}
public void setPostcodeKeyboardType(String postcodeKeyboardType) {
this.postcodeKeyboardType = postcodeKeyboardType;
}
public String getPostcodeExample() {
return postcodeExample;
}
public void setPostcodeExample(String postcodeExample) {
this.postcodeExample = postcodeExample;
}
public String getCityExample() {
return cityExample;
}
public void setCityExample(String cityExample) {
this.cityExample = cityExample;
}
}
}https://stackoverflow.com/questions/52002092
复制相似问题