Java 映射的配置属性

Java 映射的配置属性,java,spring-boot,properties,configuration,yaml,Java,Spring Boot,Properties,Configuration,Yaml,我的spring boot yaml文件中有以下结构: countryConfiguration: NL: address: postcodeKeyboardType: ALPHANUMERIC postcodeExample: 1111 AA cityExample: Amsterdam ES: address: postcodeKeyboardType: NUMERIC postcodeExample: 11

我的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;
        }
    }


}

如果我删除静态内部类配置,并放入对象,它就会工作…

我注意到地址字段的类型为
对象
。我希望它是
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;
        }
    }


}
@已验证
@组成部分
@配置属性
公共类CountryConfigurationProperties{
@NotNull
私有地图配置;
公共映射getCountryConfiguration(){
返回国配置;
}
公共无效setCountryConfiguration(映射countryConfiguration){
this.countryConfiguration=countryConfiguration;
}
公共静态类配置{
私人地址;
公共广播getAddress(){
回信地址;
}
公共无效设置地址(地址){
this.address=地址;
}
}
公共静态类地址{
私有字符串邮政编码键盘类型;
私有字符串邮政编码示例;
私有字符串cityExample;
公共字符串getPostcodeKeyboardType(){
返回邮政编码键盘类型;
}
public void setPostcodeKeyboardType(字符串postcodeKeyboardType){
this.postcodeKeyboardType=postcodeKeyboardType;
}
公共字符串getPostcodeExample(){
返回邮政编码示例;
}
public void setPostcodeExample(字符串postcodeExample){
this.postcodeExample=postcodeExample;
}
公共字符串getCityExample(){
返回城市示例;
}
公共void setCityExample(字符串cityExample){
this.cityExample=cityExample;
}
}
}
@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;
        }
    }


}