How to tell Jackson to ignore empty object during deserialization?

后端 未结 3 2057
遥遥无期
遥遥无期 2020-12-06 06:40

At the deserialization process (which as I understand is the process of converting JSON data into a Java Object), how can I tell Jackson that when it reads a object that con

3条回答
  •  悲&欢浪女
    2020-12-06 07:18

    Approach 1 : This is mostly used. @JsonInclude is used to exclude properties with empty/null/default values.Use @JsonInclude(JsonInclude.Include.NON_NULL) or @JsonInclude(JsonInclude.Include.NON_EMPTY) as per your requirement.

        @JsonInclude(JsonInclude.Include.NON_NULL)
        public class Employee {
    
            private String empId;
            private String firstName;
    
            @JsonInclude(JsonInclude.Include.NON_NULL)
            private String lastName;
            private String address;
            private String emailId;
    
       }
    

    More info about the jackson annotations : https://github.com/FasterXML/jackson-annotations/wiki/Jackson-Annotations

    Approach 2 : GSON

    use GSON (https://code.google.com/p/google-gson/)

提交回复
热议问题