DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Building a Reddit Scraper in Go: JSON Listings, Pagination, Rate Limits, and Where It Breaks
  • Exploring the DBMS_DEVELOPER Package: JSON Metadata for Oracle Objects
  • When TypeScript Types Meet Untrusted AI: Building Type-Safe LLM Pipelines With Runtime Validation
  • Replacing JSON With Protobuf in Your Microservice Mesh: A Zero-Downtime Migration Blueprint

Trending

  • PWA on iOS, Part 2: HTTPS, Manifest and Service Worker
  • Designing Frontend Applications for Failure, Not Just Success
  • Evals in CI: How to Catch Agent Regressions Before Production Does
  • Batching Strategies: Throughput vs Latency in Practice

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Coding
  3. Languages
  4. Enable/Disable Null Fields in a JSON Response

Enable/Disable Null Fields in a JSON Response

How to use JSON Include.NON_NULL flag in Spring Boot as an External Property — find out more in the article!

By 
PRAVEEN SUNDAR
·
Oct. 29, 20 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
29.7K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

Problem

We need to have a SWITCH to enable or disable the inclusion of NULL fields in the API Response in the form of property:

solutions

Solution

  • In this example, ‘RegistationResponse’ is the response object for our API. This is the object in which we want to use the ‘Enable/Disable Null fields’ flag so that the response contains NULL fields or not based on the property ‘config.response.format.include.null’ present in the application.properties file:

public class

  • Declare the property ‘config.response.format.include.null’ in application.properties file:

config response

  • Bind this property to a class-level property so that the value can be read:

@Value

  • Implement the actual logic to include nulls based on the property value by:
    • Creating an instance of com.fasterxml.jackson.databind.ObjectMapper.
    • Setting SerializationInclusion property of the created mapper to Include.NON_NULL if the global property value is false, otherwise set to Include.ALWAYS.
    • Serializing the Response object to a String using the mapper & return the String. Make sure to handle JsonProcessingException.

See below for the actual code snippet to be added while constructing the JSON Response as part of the API implementation:

regRespStr

Where regRespStr is the JSON String with NULL exclusion/inclusion applied.

Source code is available in GitHub @ this location.

Common Mistakes

Initially, I tried with the Spring Boot’s readily available approaches, which were not successful, such as:

  • Having spring.jackson.default-property-inclusion property in application.properties. This property takes values such as: always/non_null/non_absent/non_default/non_empty.
  • Extending WebMvcConfigurationSupport class and customize the Spring Boot configuration, see below:

classes

  • Creating an instance of org.springframework.http.converter.json.Jackson2ObjectMapperBuilderCustomizer, see below:

@Bean

Conclusion

The decision of whether to include the NULL fields in a JSON can be externalized using Spring Boot.

Source code is available in GitHub @ this location.

JSON

Opinions expressed by DZone contributors are their own.

Related

  • Building a Reddit Scraper in Go: JSON Listings, Pagination, Rate Limits, and Where It Breaks
  • Exploring the DBMS_DEVELOPER Package: JSON Metadata for Oracle Objects
  • When TypeScript Types Meet Untrusted AI: Building Type-Safe LLM Pipelines With Runtime Validation
  • Replacing JSON With Protobuf in Your Microservice Mesh: A Zero-Downtime Migration Blueprint

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook