Amby
Amby

Reputation: 462

Application Yml Map<String,Map<String,List<Object>>> in spring boot structure

I have a collection

@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;
    }

RefDataRespone:

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

}

I am intializing in application.yml as show below

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

I am getting an error when i trying to execute an application.

Failed to convert property value of type 'java.lang.String' to required type 'com.ptsb.ref.models.RefDataResponse' for property 'entries[GENDER][Male][0]'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'java.lang.String' to required type 'com.ptsb.ref.models.RefDataResponse' for property 'entries[GENDER][Male][0]': no matching editors or conversion strategy found

How to initialize this in application yml?

Upvotes: 0

Views: 680

Answers (1)

flyx
flyx

Reputation: 39688

This:

- refKeyTgtSystem:Siebel
  refKeyTgtSystemValue:M

defines a sequence with a single string entry, "refKeyTgtSystem:Siebel refKeyTgtSystemValue:M". When in doubt, you can use online tools using PyYAML, NimYAML (my work) or others to see how your YAML is being parsed.

The problem is that you forgot to add a space after the : characters. This is required by YAML for unquoted mapping keys. Even then, you used @JsonProperty to change the name of the keys, so the proper syntax is:

- sourceSystem: Siebel
  sourceSystemValue: M

Upvotes: 1

Related Questions