How to tell Jackson to ignore a field during serialization if its value is null?

后端 未结 19 1514
情歌与酒
情歌与酒 2020-11-22 04:55

How can Jackson be configured to ignore a field value during serialization if that field\'s value is null.

For example:

public class SomeClass {
            


        
19条回答
  •  独厮守ぢ
    2020-11-22 05:26

    We have lot of answers to this question. This answer may be helpful in some scenarios If you want to ignore the null values you can use the NOT_NULL in class level. as below

    @JsonInclude(Include.NON_NULL)
    class Foo
    {
      String bar;
    }
    

    Some times you may need to ignore the empty values such as you may have initialized the arrayList but there is no elements in that list.In that time using NOT_EMPTY annotation to ignore those empty value fields

    @JsonInclude(Include.NON_EMPTY)
    class Foo
    {
      String bar;
    }
    

提交回复
热议问题