skip to Main Content

I have id at the top level document and id at sub document. The subdocument id is not getting populated.

My JSON Document:-

{
  "_id": "a392ec9c-4f5c-411a-8599-928c5ce6b18d",
  "name": "polName",
  "rules": [
    {
      "t": "TestRule",
      "principals": [
        {
          "name": "Test Report",
          "id": "Test Report",
          "type": "PY"
        },
        {
          "name": "Test Report1",
          "id": "Test Report1",
          "type": "SY"
        }
      ]
    }
  ]
}

Dto class,

@JsonInclude(Include.NON_EMPTY)
@JsonIgnoreProperties(ignoreUnknown = true)
public class PolicyDto {

   
    @JsonProperty("docId")
    @JsonAlias({ "_id" })
    private String id;
    private String name;
    private List<Rule> rules;


    public String getId() {
        return id;
    }

    public void setId(final String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(final String name) {
        this.name = name;
    }

    public List<Rule> getRules() {
        return rules;
    }

    public void setRules(final List<Rule> rules) {
        this.rules = rules;
    }


    public static class Rule {
        private List<Principal> principals;

        public List<Principal> getPrincipals() {
            return principals;
        }

        public void setPrincipals(final List<Principal> principals) {
            this.principals = principals;
        }

    }


    public static class Principal {
        private String name;
        private String id;
        private String type;

        public String getName() {
            return name;
        }

        public void setName(final String name) {
            this.name = name;
        }

        public String getId() {
            return id;
        }

        public void setId(final String id) {
            this.id = id;
        }

        public String getType() {
            return type;
        }

        public void setType(final String type) {
            this.type = type;
        }

    }

}

Here, id inside principal is getting populated properly.

3

Answers


  1. May I know how you’re using this DTO? i.e. is this being persisted or are you sending this as a response to a request? Also, a code snippet of how you’re populating the DTO would be appreciated.

    Login or Signup to reply.
  2. I have tried the following snippet:

    public class MainClass {
    
        public static void main(String[] args) {
            String json = "{n"
                    + "  "_id": "a392ec9c-4f5c-411a-8599-928c5ce6b18d",n"
                    + "  "name": "polName",n"
                    + "  "rules": [n"
                    + "    {n"
                    + "      "t": "TestRule",n"
                    + "      "principals": [n"
                    + "        {n"
                    + "          "name": "Test Report",n"
                    + "          "id": "Test Report",n"
                    + "          "type": "PY"n"
                    + "        },n"
                    + "        {n"
                    + "          "name": "Test Report1",n"
                    + "          "id": "Test Report1",n"
                    + "          "type": "SY"n"
                    + "        }n"
                    + "      ]n"
                    + "    }n"
                    + "  ]n"
                    + "}";
            try {
                PolicyDto p = new ObjectMapper().readValue(json, PolicyDto.class);
                System.out.println("P "+p.getRules().get(0).getPrincipals().get(0).toString());
            } catch (JsonProcessingException p) {
                p.printStackTrace();
            }
        }
    }
    

    And the DTO:

    import com.fasterxml.jackson.annotation.JsonAlias;
    import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
    import com.fasterxml.jackson.annotation.JsonInclude;
    import com.fasterxml.jackson.annotation.JsonInclude.Include;
    import com.fasterxml.jackson.annotation.JsonProperty;
    import java.util.List;
    
    /**
     *
     * @author asgar
     */
    @JsonInclude(Include.NON_EMPTY)
    @JsonIgnoreProperties(ignoreUnknown = true)
    public class PolicyDto {
    
        @JsonProperty("docId")
        @JsonAlias({"_id"})
        private String id;
    
        private String name;
    
        private List<Rule> rules;
    
        public String getId() {
            return id;
        }
    
        public void setId(final String id) {
            this.id = id;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(final String name) {
            this.name = name;
        }
    
        public List<Rule> getRules() {
            return rules;
        }
    
        public void setRules(final List<Rule> rules) {
            this.rules = rules;
        }
    
        public static class Rule {
    
            private String t;
    
            private List<Principal> principals;
    
            public List<Principal> getPrincipals() {
                return principals;
            }
    
            public void setPrincipals(final List<Principal> principals) {
                this.principals = principals;
            }
    
            public String getT() {
                return t;
            }
    
            public void setT(String t) {
                this.t = t;
            }
    
        }
    
        public static class Principal {
    
            private String name;
            private String id;
            private String type;
    
            public String getName() {
                return name;
            }
    
            public void setName(final String name) {
                this.name = name;
            }
    
            public String getId() {
                return id;
            }
    
            public void setId(final String id) {
                this.id = id;
            }
    
            public String getType() {
                return type;
            }
    
            public void setType(final String type) {
                this.type = type;
            }
    
            @Override
            public String toString() {
                return "Principal{" + "name=" + name + ", id=" + id + ", type=" + type + '}';
            }
    
        }
    }
    

    The output:

    P Principal{name=Test Report, id=Test Report, type=PY}
    

    The code works fine and the id you mentioned above seems to be populated properly. How are you trying to use this DTO? Can you elaborate more?

    Login or Signup to reply.
  3. In your Json on the "Rule" level there is a field "t" – "t": "TestRule",. In your class Rule you don’t have property "t" So, when I tried to parse your JSON I got an error complaining that field "t" was not known. So I added @JsonIgnoreProperties(ignoreUnknown = true) to your Rule class and then everything worked and all the fields got populated. Here is my code:

            try {
            PolicyDto policyDto = JsonUtils.readObjectFromJsonString(jsonStr, PolicyDto.class);
        } catch (IOException ioe) {
            ...
        }
    

    Note that JsonUtils class is a thin wrapper over ObjectMapper class. JsonUtils class comes with MgntUtils Open Source Java library written and maintained by me. Here is the Javadoc for JsonUtils class. The MgntUtils library could be obtained from Maven Central or from Github (including source code and Javadoc)

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search