Since the Jackson 2.x it provides @JsonInclude
annotation that controls serialization of a class as a whole or its individual fields based on their values during serialization. It recognizes following annotations as:
Include.NON_NULL
Indicates that only non-null properties should be serialized.
Include.NON_EMPTY
Indicates that only non-null and non-empty properties should be serialized. This is actually the superset of Include.NON_NULL
Hence over a collection Include.NON_EMPTY
will work like
@JsonInclude(Include.NON_EMPTY)
private Collection field;
or you can put it over the class to impact upon the whole model like
@JsonInclude(JsonInclude.Include.NON_EMPTY)
public class Foo {
}