Why Spring Boot Sticks with Jackson as Its Default JSON Library
The article explains why Spring Boot continues to use Jackson as its default JSON serializer, detailing the automatic ObjectMapper/JsonMapper creation, the HTTP message conversion process, Jackson’s balanced strengths, practical code samples, and how to switch to alternatives like Gson, JSON‑B, or Fastjson2.
Conclusion: Default Does Not Mean Unique
Jackson has long been Spring Boot’s preferred default JSON library, though not the only option. Spring Boot 3 creates an ObjectMapper automatically; Spring Boot 4 upgrades to Jackson 3 and uses an immutable JsonMapper. The name and some APIs changed, but Spring’s choice remains.
Jackson is chosen because it balances functionality, performance, extensibility, compatibility, and ecosystem support—qualities more important for a general‑purpose web framework than raw speed.
How a Request Becomes JSON
Spring MVC does not call Jackson directly in the controller. The HTTP message converter receives the object returned by the controller, finds a suitable JSON mapper, and writes the result to the HTTP response.
When the client sends JSON, Jackson deserializes the request body into a Java object before the controller handles it. As long as the Web starter is on the classpath, the JSON starter and Jackson are pulled in automatically, enabling near‑zero‑configuration usage.
What Makes Jackson Good
1. Three Common Usage Modes
Data binding : Directly convert a User object to JSON.
Tree model : Use JsonNode to read or manipulate JSON structures like a tree.
Streaming : Read or write fields one by one, suitable for large files or low‑memory scenarios.
2. Deep Understanding of Java Types
Jackson handles dates, enums, generics, records, collections, Optional, and polymorphic types out of the box. Modules extend support—for example, a dedicated date‑time module or Kotlin module.
3. Configurable Yet Not Mandatory
Simple projects work out of the box; complex projects can fine‑tune behavior with annotations, configuration properties, custom serializers, or modules (e.g., hide passwords, rename fields, enforce a global date format).
4. Adequate Performance and Stability
Jackson may not be the absolute fastest in every benchmark, but its throughput, memory usage, and startup cost stay within mainstream levels, and it has been proven in many production systems. For most web APIs, database or network latency dominates JSON conversion.
5. Rich Spring Ecosystem Integration
Spring MVC, WebFlux, testing utilities, and numerous third‑party components integrate naturally with Jackson, reducing adapter code and easing framework upgrades.
Practical Code Samples
Controlling Field Names, Dates, and Sensitive Data
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import java.time.LocalDateTime;
/** User API response object */
public record UserResponse(
Long id,
@JsonProperty("user_name") String username,
@JsonIgnore String password,
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime createdAt
) { }Resulting JSON:
{
"id": 1,
"user_name": "小锋",
"createdAt": "2026-08-03 17:25:17"
}Global Rules via application.yml
spring:
jackson:
# Use readable date‑time format
date-format: yyyy-MM-dd HH:mm:ss
time-zone: Asia/Shanghai
default-property-inclusion: non_null
serialization:
# Do not write dates as timestamps
write-dates-as-timestamps: falseCustomizing Jackson 3 in Spring Boot 4
import org.springframework.boot.jackson.autoconfigure.JsonMapperBuilderCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.fasterxml.jackson.databind.SerializationFeature;
/** Jackson serialization configuration */
@Configuration
public class JacksonConfig {
@Bean
public JsonMapperBuilderCustomizer jsonMapperCustomizer() {
return builder -> builder.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
}
}For Spring Boot 3 the corresponding extension point is Jackson2ObjectMapperBuilderCustomizer and the core object is ObjectMapper. Upgrading requires matching the correct version.
Comparison with Other Serialization Libraries
Gson : Lightweight and easy to start, ideal for small tools or stable data structures, but lacks Jackson’s depth in polymorphism, fine‑grained customization, and Spring integration.
Fastjson2 : Emphasizes throughput and is popular in the Chinese community. It can excel in high‑load scenarios, but replacing the default requires manual message‑converter replacement and careful compatibility checks, especially regarding security differences between Fastjson and Fastjson2.
JSON‑B : A Jakarta standard offering portability and replaceability. Spring Boot can auto‑configure it, yet Jackson remains more mature and better supported in the Spring ecosystem.
Kryo : Fast binary serializer for Java objects, suited for caching or RPC, not a direct replacement for browser‑facing JSON.
In short, Gson wins on simplicity, Fastjson2 on raw speed, JSON‑B on standard compliance, while Jackson provides a well‑rounded solution that aligns with Spring Boot’s default selection logic.
How to Configure an Alternative Library
Spring Boot does not hard‑wire Jackson. For Spring Boot 4, Gson and JSON‑B have official starters and auto‑configuration; Fastjson2 lacks an official starter and requires manual HTTP‑message‑converter replacement.
Switch to Gson
<!-- Use Gson for JSON -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-gson</artifactId>
</dependency> spring:
http:
converters:
# Prefer Gson as the JSON mapper
preferred-json-mapper: gson
gson:
# Global date‑time format
date-format: yyyy-MM-dd HH:mm:ssWhen Gson is on the classpath, Spring Boot creates a Gson bean. For finer control, provide a GsonBuilderCustomizer bean.
import org.springframework.boot.gson.autoconfigure.GsonBuilderCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class GsonConfig {
@Bean
public GsonBuilderCustomizer gsonBuilderCustomizer() {
return builder -> builder.serializeNulls()
.disableHtmlEscaping();
}
}Switch to JSON‑B
<!-- Use Jakarta JSON‑B -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jsonb</artifactId>
</dependency> spring:
http:
converters:
# Prefer JSON‑B as the JSON mapper
preferred-json-mapper: jsonbWhen using JSON‑B, replace Jackson‑specific annotations with @JsonbProperty, @JsonbDateFormat, etc.
Switch to Fastjson2
<!-- Fastjson2 core library -->
<dependency>
<groupId>com.alibaba.fastjson2</groupId>
<artifactId>fastjson2</artifactId>
<version>${fastjson2.version}</version>
</dependency>
<!-- Fastjson2 Spring MVC extension -->
<dependency>
<groupId>com.alibaba.fastjson2</groupId>
<artifactId>fastjson2-extension</artifactId>
<version>${fastjson2.version}</version>
</dependency> import com.alibaba.fastjson2.support.config.FastJsonConfig;
import com.alibaba.fastjson2.support.spring.http.converter.FastJsonHttpMessageConverter;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverters;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class Fastjson2Config implements WebMvcConfigurer {
@Override
public void configureMessageConverters(HttpMessageConverters.ServerBuilder builder) {
FastJsonConfig config = new FastJsonConfig();
config.setDateFormat("yyyy-MM-dd HH:mm:ss");
FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
converter.setFastJsonConfig(config);
builder.jsonMessageConverter(converter);
}
}For Spring Boot 3 / Spring Framework 6 the API uses List<HttpMessageConverter<?>> instead of the Spring 7 builder shown above.
Regardless of the chosen library, regression‑test dates, enums, null values, generic collections, and error responses, as edge‑case data often reveals hidden issues.
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.
java1234
Former senior programmer at a Fortune Global 500 company, dedicated to sharing Java expertise. Visit Feng's site: Java Knowledge Sharing, www.java1234.com
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.
