【问题标题】:Application Yml Map<String,Map<String,List<Object>>> in spring boot structureSpring Boot 结构中的应用 Yml Map<String,Map<String,List<Object>>>
【发布时间】:2020-07-14 03:08:24
【问题描述】:

我有一个收藏

@Component
@ConfigurationProperties(prefix="com.ptsb.refData")
public class RefDataConfigMap {

    private Map<String,Map<String,List<RefDataResponse>>> entries;

    public Map<String, Map<String, List<RefDataResponse>>> getEntries() {
        return entries;
    }

    public void setEntries(Map<String, Map<String, List<RefDataResponse>>> entries) {
        this.entries = entries;
    }

参考数据响应:

public class RefDataResponse {

      @JsonProperty("sourceSystem")
      private String refKeyTgtSystem = null;

      @JsonProperty("sourceSystemValue")
      private String refKeyTgtSystemValue = null;

    public String getRefKeyTgtSystem() {
        return refKeyTgtSystem;
    }

    public void setRefKeyTgtSystem(String refKeyTgtSystem) {
        this.refKeyTgtSystem = refKeyTgtSystem;
    }

    public String getRefKeyTgtSystemValue() {
        return refKeyTgtSystemValue;
    }

    public void setRefKeyTgtSystemValue(String refKeyTgtSystemValue) {
        this.refKeyTgtSystemValue = refKeyTgtSystemValue;
    }

}

我正在 application.yml 中初始化,如下所示

com:
  ptsb:
    refData:
      entries:
             GENDER:
                    Male:
                         - refKeyTgtSystem:Siebel
                           refKeyTgtSystemValue:M

我在尝试执行应用程序时遇到错误。

未能将类型“java.lang.String”的属性值转换为属性“条目[GENDER][Male][0]”所需的类型“com.ptsb.ref.models.RefDataResponse”;嵌套异常是 java.lang.IllegalStateException:无法将类型 'java.lang.String' 的值转换为属性 'entries[GENDER][Male][0]' 所需的类型 'com.ptsb.ref.models.RefDataResponse':未找到匹配的编辑器或转换策略

如何在应用 yml 中初始化这个?

【问题讨论】:

    标签: java spring-boot collections yaml


    【解决方案1】:

    这个:

    - refKeyTgtSystem:Siebel
      refKeyTgtSystemValue:M
    

    定义一个带有单个字符串条目的序列,"refKeyTgtSystem:Siebel refKeyTgtSystemValue:M"。如有疑问,您可以使用在线工具using PyYAMLNimYAML(我的工作)或其他工具来查看您的 YAML 是如何被解析的。

    问题是您忘记在: 字符后添加一个空格。对于未引用的映射键,这是 YAML 所要求的。即便如此,您还是使用了@JsonProperty 来更改键的名称,因此正确的语法是:

    - sourceSystem: Siebel
      sourceSystemValue: M
    

    【讨论】:

    • 它仅与 refKeyTgtSystem 和值一起使用,但正如您所说,我应该在 : 字符之后添加一个空格。现在它正在工作,谢谢!!!
    • 啊,可能是因为 @JsonProperty 是 Jackson 的东西,但你使用的是 Spring Boot。
    猜你喜欢
    • 2018-09-22
    • 2020-08-01
    • 1970-01-01
    • 2020-01-26
    • 1970-01-01
    • 2021-04-29
    • 1970-01-01
    • 2015-11-21
    • 1970-01-01
    相关资源
    最近更新 更多