Jackson serialization: ignore empty values (or null)
You need to add import com.fasterxml.jackson.annotation.JsonInclude;
Add
@JsonInclude(JsonInclude.Include.NON_NULL)
on top of POJO
If you have nested POJO then
@JsonInclude(JsonInclude.Include.NON_NULL)
need to add on every values.
NOTE: JAXRS (Jersey) automatically handle this scenario 2.6 and above.
You have the annotation in the wrong place - it needs to be on the class, not the field. i.e:
@JsonInclude(Include.NON_NULL) //or Include.NON_EMPTY, if that fits your use case
public static class Request {
// ...
}
As noted in comments, in versions below 2.x the syntax for this annotation is:
@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL) // or JsonSerialize.Inclusion.NON_EMPTY
The other option is to configure the ObjectMapper
directly, simply by calling
mapper.setSerializationInclusion(Include.NON_NULL);
(for the record, I think the popularity of this answer is an indication that this annotation should be applicable on a field-by-field basis, @fasterxml)
Also you can try to use
@JsonSerialize(include=JsonSerialize.Inclusion.NON_NULL)
if you are dealing with jackson with version below 2+ (1.9.5) i tested it, you can easily use this annotation above the class. Not for specified for the attributes, just for class decleration.
You can also set the global option:
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);