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

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Low-Code Development: Leverage low and no code to streamline your workflow so that you can focus on higher priorities.

DZone Security Research: Tell us your top security strategies in 2024, influence our research, and enter for a chance to win $!

Launch your software development career: Dive head first into the SDLC and learn how to build high-quality software and teams.

Open Source Migration Practices and Patterns: Explore key traits of migrating open-source software and its impact on software development.

Related

  • Flask Web Application for Smart Honeypot Deployment Using Reinforcement Learning
  • Terraform Tips for Efficient Infrastructure Management
  • Flexible Data Generation With Datafaker Gen
  • Keep Calm and Column Wise

Trending

  • Transforming Software Development With Low-Code and No-Code Integration
  • From Backlog Manager to Product Manager [Video]
  • Javac and Java Katas, Part 1: Class Path
  • Documenting a Spring REST API Using Smart-doc
  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
Like (3)
Save
Tweet
Share
24.5K 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

  • Flask Web Application for Smart Honeypot Deployment Using Reinforcement Learning
  • Terraform Tips for Efficient Infrastructure Management
  • Flexible Data Generation With Datafaker Gen
  • Keep Calm and Column Wise

Partner Resources


Comments

ABOUT US

  • About DZone
  • Send feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: