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

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

    Just to expand on the other answers - if you need to control the omission of null values on a per-field basis, annotate the field in question (or alternatively annotate the field's 'getter').

    example - here only fieldOne will be ommitted from json if it is null. fieldTwo will always be included regardless of if it is null.

    public class Foo {
    
        @JsonInclude(JsonInclude.Include.NON_NULL) 
        private String fieldOne;
    
        private String fieldTwo;
    }
    

    To omit all null values in the class as a default, annotate the class. Per-field/getter annotations can still be used to override this default if necessary.

    example - here fieldOne and fieldTwo will be ommitted from json if they are null, respectively, because this is the default set by the class annotation. fieldThree however will override the default and will always be included, because of the annotation on the field.

    @JsonInclude(JsonInclude.Include.NON_NULL)
    public class Foo {
    
        private String fieldOne;
    
        private String fieldTwo;
    
        @JsonInclude(JsonInclude.Include.ALWAYS)
        private String fieldThree;
    }
    

    UPDATE

    The above is for Jackson 2. For earlier versions of Jackson you need to use:

    @JsonSerialize(include=JsonSerialize.Inclusion.NON_NULL) 
    

    instead of

    @JsonInclude(JsonInclude.Include.NON_NULL)
    

    If this update is useful, please upvote ZiglioUK's answer below, it pointed out the newer Jackson 2 annotation long before I updated my answer to use it!

    0 讨论(0)
  • 2020-11-22 05:17

    in my case

    @JsonInclude(Include.NON_EMPTY)
    

    made it work.

    0 讨论(0)
  • 2020-11-22 05:19

    Also, you have to change your approach when using Map myVariable as described in the documentation to eleminate nulls:

    From documentation:
    com.fasterxml.jackson.annotation.JsonInclude
    
    @JacksonAnnotation
    @Target(value={ANNOTATION_TYPE, FIELD, METHOD, PARAMETER, TYPE})
    @Retention(value=RUNTIME)
    Annotation used to indicate when value of the annotated property (when used for a field, method or constructor parameter), or all properties of the annotated class, is to be serialized. Without annotation property values are always included, but by using this annotation one can specify simple exclusion rules to reduce amount of properties to write out.
    
    *Note that the main inclusion criteria (one annotated with value) is checked on Java object level, for the annotated type, and NOT on JSON output -- so even with Include.NON_NULL it is possible that JSON null values are output, if object reference in question is not `null`. An example is java.util.concurrent.atomic.AtomicReference instance constructed to reference null value: such a value would be serialized as JSON null, and not filtered out.
    
    To base inclusion on value of contained value(s), you will typically also need to specify content() annotation; for example, specifying only value as Include.NON_EMPTY for a {link java.util.Map} would exclude Maps with no values, but would include Maps with `null` values. To exclude Map with only `null` value, you would use both annotations like so:
    public class Bean {
       @JsonInclude(value=Include.NON_EMPTY, content=Include.NON_NULL)
       public Map<String,String> entries;
    }
    
    Similarly you could Maps that only contain "empty" elements, or "non-default" values (see Include.NON_EMPTY and Include.NON_DEFAULT for more details).
    In addition to `Map`s, `content` concept is also supported for referential types (like java.util.concurrent.atomic.AtomicReference). Note that `content` is NOT currently (as of Jackson 2.9) supported for arrays or java.util.Collections, but supported may be added in future versions.
    Since:
    2.0
    
    0 讨论(0)
  • 2020-11-22 05:22

    With Jackson > 1.9.11 and < 2.x use @JsonSerialize annotation to do that:

    @JsonSerialize(include=JsonSerialize.Inclusion.NON_NULL)

    0 讨论(0)
  • 2020-11-22 05:22

    In Jackson 2.x, use:

    @JsonInclude(JsonInclude.Include.NON_NULL)
    
    0 讨论(0)
  • 2020-11-22 05:23

    If you want to add this rule to all models in Jackson 2.6+ use:

    mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
    
    0 讨论(0)
提交回复
热议问题