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

后端 未结 19 1525
情歌与酒
情歌与酒 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:25

    This has been troubling me for quite some time and I finally found the issue. The issue was due to a wrong import. Earlier I had been using

    com.fasterxml.jackson.databind.annotation.JsonSerialize
    

    Which had been deprecated. Just replace the import by

    import org.codehaus.jackson.map.annotate.JsonSerialize;
    import org.codehaus.jackson.map.annotate.JsonSerialize.Inclusion;
    

    and use it as

    @JsonSerialize(include=Inclusion.NON_NULL)
    

提交回复
热议问题