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
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Stop Poisoning Your Models: How I Built a CV Dataset Quality Toolkit I Can Reuse Forever
  • Lambda-Driven API Design: Building Composable Node.js Endpoints With Functional Primitives
  • The Update Problem REST Doesn't Solve
  • Designing a Production-Grade Multi-Agent LLM Architecture for Structured Data Extraction

Trending

  • Designing API-First EMR Architectures in .NET: Enabling Modular Growth in Compliance-Driven Systems
  • Architecting an Embedded Efficiency Layer: A Platform Deep Dive into Day-Two Operational Tuning
  • A Walk-Through of the DZone Article Editor
  • Docker Hardened Images Are Free Now — Here's What You Still Need to Build
  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 user avatar
PRAVEEN SUNDAR
·
Oct. 29, 20 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
29.2K Views

Join the DZone community and get the full member experience.

Join For Free

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

  • Stop Poisoning Your Models: How I Built a CV Dataset Quality Toolkit I Can Reuse Forever
  • Lambda-Driven API Design: Building Composable Node.js Endpoints With Functional Primitives
  • The Update Problem REST Doesn't Solve
  • Designing a Production-Grade Multi-Agent LLM Architecture for Structured Data Extraction

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • 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