Spring Boot JSON Handling: Default Jackson, FastJson, and Unified Response Structure
This tutorial explains how Spring Boot returns JSON using the default Jackson library, compares it with Alibaba's FastJson, demonstrates handling of null values, and shows how to encapsulate responses in a unified structure with status codes and messages for backend development.
In Spring Boot projects, JSON is the common data exchange format between front‑end and back‑end, and the framework provides simple ways to return JSON from controllers using the @RestController annotation, which combines @Controller and @ResponseBody.
The default JSON processor is Jackson, included via spring-boot-starter-json and its transitive dependencies such as jackson-databind, jackson-datatype-jdk8, jackson-datatype-jsr310, and jackson-module-parameter-names. To illustrate JSON conversion, a User entity class is created and a JsonController returns a User object, a List<User>, and a Map<String, Object>. Sample requests to localhost:8080/json/user, /json/list, and /json/map show the resulting JSON structures.
When null values appear, Jackson can be configured to serialize them as empty strings by defining a JacksonConfig class that provides a custom ObjectMapper bean with a JsonSerializer<Object> that writes "" for nulls. After updating the map endpoint to contain null fields and restarting the application, the JSON output shows empty strings instead of nulls.
Alibaba's FastJson can be used as an alternative. A comparison table highlights differences in ease of use, feature richness, documentation language, and speed. To use FastJson, add the Maven dependency:
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.35</version>
</dependency>Then create a configuration class extending WebMvcConfigurationSupport that overrides configureMessageConverters to register a FastJsonHttpMessageConverter with serializer features such as WriteMapNullValue, WriteNullStringAsEmpty, WriteNullNumberAsZero, WriteNullListAsEmpty, and WriteNullBooleanAsFalse.
For larger projects, a unified response wrapper JsonResult<T> is introduced. It contains fields data, code, and msg, with constructors for default success, custom code/message, data‑only success, and data with custom message. Controllers can return JsonResult objects instead of raw data, providing consistent status codes and messages.
Updated controller methods demonstrate returning a single User, a list of users, and a map with possible null values, all wrapped in JsonResult. The resulting JSON includes the code, data, and msg fields, making API responses more informative.
The article concludes that Spring Boot’s default Jackson handling, optional FastJson integration, and a custom unified response structure together offer a comprehensive solution for JSON data exchange in backend development.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Full-Stack Internet Architecture
Introducing full-stack Internet architecture technologies centered on Java
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
