Jackson: ignore null or empty fields with @JsonInclude NON_NULL and NON_EMPTY
Codemia Knowledge Hub ·
Annotate the class or field with @JsonInclude(JsonInclude.Include.NON_NULL) to make Jackson skip null values, or use Include.NON_EMPTY to also skip empty strings, empty collections and empty arrays. To apply a rule everywhere, set it on the ObjectMapper with setSerializationInclusion. Which one to choose depends on whether an empty value means something in your API or is just noise.
Skip only null fields with NON_NULL
If email is null, it is left out of the JSON. If it is an empty string, it is still serialized: NON_NULL does not suppress empty strings or empty collections.
Skip empty values too with NON_EMPTY
With NON_EMPTY, Jackson omits:
nullfields""- empty lists
- empty arrays
That is convenient only if the omissions match the contract your clients expect.
Set the rule globally on the ObjectMapper
Instead of annotating every class, configure the mapper once:
The rule then applies to everything that mapper serializes. This suits APIs with one consistent serialization policy, but check that all your DTOs really share the same semantics first.
Override the rule on one field
When one field needs different behavior from the rest, annotate just that field. Field-level annotations override a broader class or mapper setting:
This helps when an empty string or empty collection must be preserved on some fields but not others.
Example: NON_EMPTY output
Only the non-empty data appears in the output.
Decide what a missing field means
Choose inclusion rules at the DTO or mapper boundary rather than deep inside domain objects. That keeps the serialization policy visible and stops one team's payload cleanup from changing another endpoint's meaning.
An omitted field can mean very different things:
- value unknown
- value intentionally absent
- empty but insignificant
- omitted only to reduce payload size
If consumers distinguish between null, an empty string and a missing field, NON_EMPTY may be the wrong choice. Pick the rule based on API semantics, not only on output size.
Related reading
- Jackson serializes a ZonedDateTime wrongly in Spring Boot
- Jackson Vs. Gson
- Jackson with JSON Unrecognized field, not marked as ignorable
- Java-R integration?
- Java8 Why is it forbidden to define a default method for a method from java.lang.Object
- Java - Convert integer to string
- Java - escape string to prevent SQL injection
- Java - get the current class name?
System design interview problems on this topic
- Design an API Rate Limiter: solution and walkthrough
- Design a Distributed Messaging System: solution and walkthrough
- Design an Online Payment Service: solution and walkthrough
- Design a Key Value Store: solution and walkthrough

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.