HiveBrain v1.2.0
Get Started
← Back to all entries
snippetjavaCritical

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

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
howduringfieldserializationjacksonignoreitsvaluetellnull

Problem

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; 
}

Solution

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.

Code Snippets

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

Context

Stack Overflow Q#11757487, score: 1374

Revisions (0)

No revisions yet.