【问题标题】:Loading HashMap to a nested configuration bean throws Binding Exception将 HashMap 加载到嵌套配置 bean 会引发绑定异常
【发布时间】:2019-06-26 07:30:05
【问题描述】:

我正在尝试这里给出的这个例子: https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-external-config-typesafe-configuration-properties

除了尝试添加另一个属性来加载 hashmap 值之外,一切都运行良好

属性添加为:

    demoapp.security.policies={'KEY1': 'value1', 'KEY2': 'value3', 'KEY3': 'value5'}

并且在 Securiry 内部类中,添加了另一个变量,如下所示:

private Map<String, String> policies;

public Map<String, String> getPolicies() {
  return policies;
}

public void setPolicies(Map<String, String> policies) {
  this.policies = policies;
}

但这会引发错误:

    Caused by: org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [java.lang.String] to type [java.util.Map<java.lang.String, java.lang.String>]

有趣的是,如果我把它放在一个普通的(非嵌套的)配置类中,它对我来说很好。

这里出了什么问题,请有任何建议

【问题讨论】:

    标签: java spring spring-boot-configuration


    【解决方案1】:

    当绑定到地图时,你是在绑定嵌套属性,所以你需要单独指定属性。

    属性文件:

    demoapp.security.policies.KEY1=value1
    demoapp.security.policies.KEY2=value3
    demoapp.security.policies.KEY3=value5
    

    YAML 文件:

    demoapp.security.policies:
      "[KEY1]": value1
      "[KEY2]": value3
      "[KEY3]": value5
    

    【讨论】:

    • 已解决,感谢@Andreas 的领导。赞赏。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-20
    • 2018-05-29
    相关资源
    最近更新 更多