首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >地图的配置属性

地图的配置属性
EN

Stack Overflow用户
提问于 2018-08-24 18:04:14
回答 1查看 2.8K关注 0票数 3

我在我的spring boot yaml文件中有以下结构:

代码语言:javascript
复制
countryConfiguration:
  NL:
    address:
      postcodeKeyboardType: ALPHANUMERIC
      postcodeExample: 1111 AA
      cityExample: Amsterdam
  ES:
    address:
      postcodeKeyboardType: NUMERIC
      postcodeExample: 11111
      cityExample: Madrid

我想创建一个配置属性类来访问这些值。我有这样的东西:

代码语言:javascript
复制
@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失败:

代码语言:javascript
复制
Property: .countryConfiguration
Value: null
Reason: must not be null

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-08-24 21:52:37

我注意到address字段的类型为Object。我希望它是Address类型,并且应该有一个表示Address对象的内部类。

在下面的代码片段中,我添加了一个Address类来匹配您正在使用的yml配置。我对此进行了测试,它成功启动并相应地映射了属性。

代码语言:javascript
复制
@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;
        }
    }


}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52002092

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档