I have the following structure in my spring boot yaml file:
countryConfiguration:
NL:
address:
postcodeKeyboardType: ALPHANUMERIC
postcodeExample: 1111 AA
cityExample: Amsterdam
ES:
address:
postcodeKeyboardType: NUMERIC
postcodeExample: 11111
cityExample: Madrid
And I want to create a configuration properties class to access to those values. I have something like this:
@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;
}
}
}
But it does not work, I get this: Binding to target org.springframework.boot.context.properties.bind.BindException: Failed to bind properties under '' to io.bux.onboarding.application.config.CountryConfigurationProperties$$EnhancerBySpringCGLIB$$1d9a5856 failed:
Property: .countryConfiguration
Value: null
Reason: must not be null
If I remove the static inner class Configuration, and I put Object, it works...
I noticed that the address field has type Object. I would expect this to be of type Address and for there to be an inner class that represents the Address object.
In the code snippet below I have added an Address class to match the yml config you are using. I have tested this and it starts up successfully and maps the properties accordingly.
@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;
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With