How to tell Jackson to ignore a field during seria

2018-12-31 12:22发布

问题:

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

For example:

public class SomeClass {
   // what jackson annotation causes jackson to skip over this value if it is null but will 
   // serialize it otherwise 
   private String someValue; 
}

回答1:

To suppress serializing properties with null values using Jackson >2.0, you can configure the ObjectMapper directly, or make use of the @JsonInclude annotation:

mapper.setSerializationInclusion(Include.NON_NULL);

or:

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

Alternatively, you could use @JsonInclude in a getter so that the attribute would be shown if the value is not null.

A more complete example is available in my answer to How to prevent null values inside a Map and null fields inside a bean from getting serialized through Jackson.



回答2:

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

@JsonSerialize(include=JsonSerialize.Inclusion.NON_NULL)



回答3:

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!



回答4:

In Jackson 2.x, use:

@JsonInclude(JsonInclude.Include.NON_NULL)


回答5:

You can use the following mapper configuration:

mapper.getSerializationConfig().setSerializationInclusion(Inclusion.NON_NULL);

Since 2.5 you can user:

mapper.setSerializationInclusion(Include.NON_NULL);


回答6:

You can set application.properties:

spring.jackson.default-property-inclusion=non_null

or application.yaml:

spring:
  jackson:
    default-property-inclusion: non_null

http://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html



回答7:

in my case

@JsonInclude(Include.NON_EMPTY)

made it work.



回答8:

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonInclude(JsonInclude.Include.NON_EMPTY)

should work.

Include.NON_EMPTY indicates that property is serialized if its value is not null and not empty. Include.NON_NULL indicates that property is serialized if its value is not null.



回答9:

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

mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);


回答10:

If in Spring Boot, you can customize the jackson ObjectMapper directly through property files.

Example application.yml:

spring:
  jackson:
    default-property-inclusion: non_null # only include props if non-null

Possible values are:

always|non_null|non_absent|non_default|non_empty

More: https://docs.spring.io/spring-boot/docs/current/reference/html/howto-spring-mvc.html#howto-customize-the-jackson-objectmapper



回答11:

For Jackson 2.5 use :

@JsonInclude(content=Include.NON_NULL)


回答12:

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)


回答13:

If you\'re trying to serialize a list of object and one of them is null you\'ll end up including the null item in the json even with

mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);

will result in:

[{myObject},null]

to get this:

[{myObject}]

one can do something like:

mapper.getSerializerProvider().setNullValueSerializer(new JsonSerializer<Object>() {
        @Override
        public void serialize(Object obj, JsonGenerator jsonGen, SerializerProvider unused)
                throws IOException
        {
            //IGNORES NULL VALUES!
        }
    });

TIP: If you\'re using DropWizard you can retrieve the ObjectMapper being used by Jersey using environment.getObjectMapper()



回答14:

Global configuration if you use Spring

@Configuration
public class JsonConfigurations {

    @Bean
    public Jackson2ObjectMapperBuilder objectMapperBuilder() {
        Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
        builder.serializationInclusion(JsonInclude.Include.NON_NULL);
        builder.serializationInclusion(JsonInclude.Include.NON_EMPTY);
        builder.failOnUnknownProperties(false);
        return builder;
    }

}


回答15:

Jackson 2.x+ use

mapper.getSerializationConfig().withSerializationInclusion(JsonInclude.Include.NON_NULL);


回答16:

This Worked for me in Spring boot 2.0.3 and Jackson 2.0

import com.fasterxml.jackson.annotation.JsonInclude;

@JsonInclude(JsonInclude.Include.NON_NULL)
public class ApiDTO
{
    // your class variable and 
    // methods
}


标签: java jackson