Null values come up so much in software program improvement, and correct dealing with of null
values may be changed into a science in and of itself. Jackson is the de-facto library for serialization and deserialization of Java objects – and a standard situation pertains to serializing objects which have null
fields.
On this quick tutorial, we’ll check out how one can ignore null fields when serializing a POJO with Jackson.
Altering the ObjectMapper Settings
Jackson’s ObjectMapper is the central API that maps objects to serialized values. Naturally, you possibly can alter and customise the way in which it really works via numerous flags, one in all which is:
objectMapper.setSerializationInclusion(Embrace.NON_NULL);
When serializing with this mapper, properties will solely be included in the event that they’re non-null.
This units the flag globally for any object being serialized with the objectMapper
occasion. Should you’re working with Spring Boot, and the ObjectMapper it is counting on is not uncovered, you possibly can alter the flag within the software.properties
file:
spring.jackson.default-property-inclusion = non_null
Or, if you happen to’re utilizing software.yml
:
spring:
jackson:
default-property-inclusion: non_null
Be aware: Older variations of Spring used spring.jackson.serialization-inclusion=non_null
as a substitute of spring.jackson.default-property-inclusion=non_null
.
Alternatively, you possibly can peel again substitute the default ObjectMapper
occasion by registering a brand new @Major
@Bean
, named objectMapper()
, which overrides the default implementation and permits full customization over the occasion that’ll be utilized by default when serializing and deserializing:
@Bean
@Major
public ObjectMapper objectMapper() {
return new ObjectMapper()
.setSerializationInclusion(JsonInclude.Embrace.NON_NULL);
}
This @Bean
ought to go into any file that is annotated with the @Configuration
annotation, and is oftentimes located both in a specialised configuration class or the primary class of the Spring Boot software.
Altering the Jackson2ObjectMapperBuilder Occasion
Spring makes use of the HttpMessageConverters
to transform between HTTP messages and objects. The default converters are the ObjectMapper
and XmlMapper
situations instantiated by way of the Jackson2ObjectMapperBuilder
.
As an alternative of manually offering the first ObjectMapper
(changing the one offered by the builder), you possibly can instruct Spring on tips on how to construct the article mapper as a substitute. This permits Spring to do its high-level configurations with out you tampering with the lower-level objects, whereas nonetheless customizing the way in which it really works. Moreover, you can even customise the XmlMapper
this manner in a single go.
To customise the builder, you register a brand new @Bean
of Jackson2ObjectMapperBuilderCustomizer
kind, which customizes and returns the builder:
@Bean
public Jackson2ObjectMapperBuilderCustomizer customBuilder() {
return builder -> {
builder.serializationInclusion(JsonInclude.Embrace.NON_NULL);
}
}
@JsonInclude Annotation
On the opposite finish of the spectrum, we will use annotations, as a low-code different! Annotations may be utilized on the class-level or method-level:
@JsonInclude(JsonInclude.Embrace.NON_NULL)
This lets you customise solely sure lessons, as a substitute of the worldwide conduct of the article mapper.
Try our hands-on, sensible information to studying Git, with best-practices, industry-accepted requirements, and included cheat sheet. Cease Googling Git instructions and truly study it!
Be aware: As of Jackson 2.0, the annotation’s worth is JsonInclude.Embrace.NON_NULL
. For earlier variations, use embrace=JsonSerialize.Inclusion.NON_NULL
.
@JsonInclude on Class-Stage
While you apply @JsonInclude
at class-level, the annotation extends to all the getters and setters:
@Entity(identify="property")
@JsonInclude(JsonInclude.Embrace.NON_NULL)
public class Property implements Serializable {
non-public static remaining lengthy serialVersionUID = 1L;
@Id
@GeneratedValue(technique=GenerationType.AUTO)
non-public String id;
@Column(identify="transaction_type")
non-public TransactionType transactionType;
@Column(identify="property_type")
non-public PropertyType propertyType;
}
Any given subject that has a null
worth will not be serialized.
@JsonInclude on Methodology-Stage
Alternatively, you possibly can apply the annotation at method-level as essentially the most granular choice:
@Entity(identify="property")
public class Property implements Serializable {
non-public static remaining lengthy serialVersionUID = 1L;
@Id
@GeneratedValue(technique=GenerationType.AUTO)
non-public String id;
@Column(identify="transaction_type")
@JsonInclude(JsonInclude.Embrace.NON_NULL)
non-public TransactionType transactionType;
@Column(identify="property_type")
non-public PropertyType propertyType;
}
On this snippet – the annotation is just utilized to the TransactionType
subject.
Conclusion
On this information, you’ve got realized tips on how to ignore null fields in Jackson, with Java and Spring Boot. This may be achieved globally be changing the underlying implicit ObjectMapper
occasion and customizing the conduct of the brand new main bean you register.
Alternatively, you possibly can customise the Jackson2ObjectMapperBuilder
that builds the ObjectMapper
on your software as a substitute of changing the already constructed one. For a low-code resolution – you possibly can change the spring.jackson.default-property-inclusion
property in your properties information.
Lastly – you should utilize the @JsonInclude
annotation on class-level or method-level.