JSON is one of the most common formats used to exchange data between applications, especially in REST APIs.
For example, a Java object:
public class User {
private String name;
private int age;
}
can be represented as JSON:
{
"name": "Ravi",
"age": 25
}
The process of converting Java data to JSON is called serialization.
The reverse process—converting JSON into Java data—is called deserialization.
Spring Boot provides integration with several JSON mapping libraries:
| Library | Current Boot 4.1.1 status |
|---|---|
| Jackson 3 | Preferred and default. |
| Jackson 2 | Deprecated; provided mainly to ease migration to Jackson 3. |
| Gson | Supported through auto-configuration. |
| JSON-B | Supported through auto-configuration. |
| Kotlin Serialization | Supported through auto-configuration. |
A JSON mapper converts between application objects and JSON data.
Java Object
↓
JSON Mapper
↓
JSON
and:
JSON
↓
JSON Mapper
↓
Java Object
In Spring Boot 4.1.1, the default Jackson 3 integration automatically configures a JsonMapper bean when Jackson is available on the classpath.
ObjectMapper as the default.Spring Boot provides auto-configuration for Jackson 3.
Jackson is included in spring-boot-starter-json. When Jackson is on the classpath, Spring Boot automatically configures a JsonMapper bean.
spring-boot-starter-json
↓
Jackson 3
↓
Spring Boot auto-configuration
↓
JsonMapper
JsonMapper just to start working with JSON in Spring Boot.Serialization means converting an in-memory Java value into JSON.
User object
{
name = "Ravi",
age = 25
}
↓
serialize
↓
{
"name": "Ravi",
"age": 25
}
Serialization is commonly involved when a REST endpoint returns an object as its response body.
Deserialization means converting JSON into an application object.
{
"name": "Ravi",
"age": 25
}
↓
deserialize
↓
User object
It is commonly involved when a REST endpoint receives JSON in a request body.
Sometimes the default mapping is not exactly what your application needs.
For example, you may want:
Spring Boot's current Jackson 3 documentation provides ValueSerializer and ValueDeserializer as the relevant extension types.
Spring Boot provides the @JacksonComponent annotation as a convenient way to register custom Jackson serializers and deserializers as Spring beans.
import org.springframework.boot.jackson.JacksonComponent;
@JacksonComponent
public class MyJacksonComponent {
// serializers and deserializers
}
The annotation can be placed directly on:
ValueSerializer implementationsValueDeserializer implementationsKeyDeserializer implementations@JacksonComponent = “Register my Jackson customization as a Spring bean.”Spring Boot automatically registers @JacksonComponent beans with Jackson.
The annotation is meta-annotated with @Component, so normal Spring component scanning rules apply.
@JacksonComponent
↓
Spring component scanning
↓
ApplicationContext
↓
Jackson registration
A simplified Jackson 3 serializer can look like this:
import tools.jackson.core.JsonGenerator;
import tools.jackson.databind.SerializationContext;
import tools.jackson.databind.ValueSerializer;
import org.springframework.boot.jackson.JacksonComponent;
@JacksonComponent
public class MyJacksonComponent {
public static class Serializer
extends ValueSerializer<MyObject> {
@Override
public void serialize(
MyObject value,
JsonGenerator jgen,
SerializationContext context) {
jgen.writeStartObject();
jgen.writeStringProperty("name", value.getName());
jgen.writeNumberProperty("age", value.getAge());
jgen.writeEndObject();
}
}
}
This custom serializer controls exactly how MyObject becomes JSON.
public static class Deserializer
extends ValueDeserializer<MyObject> {
@Override
public MyObject deserialize(
JsonParser jsonParser,
DeserializationContext ctxt) {
JsonNode tree = jsonParser.readValueAsTree();
String name = tree.get("name").stringValue();
int age = tree.get("age").intValue();
return new MyObject(name, age);
}
}
The deserializer reads JSON and constructs the Java object.
Spring Boot also provides:
These provide useful alternatives to the standard Jackson serializer/deserializer base classes.
The serializer example can therefore be simplified:
@JacksonComponent
public class MyJacksonComponent {
public static class Serializer
extends ObjectValueSerializer<MyObject> {
@Override
protected void serializeObject(
MyObject value,
JsonGenerator jgen,
SerializationContext context) {
jgen.writeStringProperty("name", value.getName());
jgen.writeNumberProperty("age", value.getAge());
}
}
}
The corresponding deserializer can use:
public static class Deserializer
extends ObjectValueDeserializer<MyObject> {
@Override
protected MyObject deserializeObject(
JsonParser jsonParser,
DeserializationContext context,
JsonNode tree) {
String name =
nullSafeValue(tree.get("name"), String.class);
int age =
nullSafeValue(tree.get("age"), Integer.class);
return new MyObject(name, age);
}
}
| Situation | Why customize? |
|---|---|
| Legacy JSON format | Match an existing external API. |
| Special field representation | Control exactly what JSON contains. |
| Custom deserialization | Convert unusual JSON structures into domain objects. |
| API compatibility | Keep your Java model independent from an external JSON contract. |
Jackson supports mixins. A mixin lets you add Jackson annotations to a target class without modifying that target class directly.
This is especially useful when:
Spring Boot's Jackson auto-configuration scans application packages for classes annotated with @JacksonMixin.
Those mixins are automatically registered with the auto-configured JsonMapper.
@JacksonMixin
public class MyObjectMixin {
// Jackson annotations
}
Spring Boot performs the registration through JacksonMixinModule.
@JacksonMixin = “Apply Jackson annotations to another class without changing that class directly.”Spring Boot 4.1.1 still provides deprecated Jackson 2 auto-configuration through the spring-boot-jackson2 module.
When that module is on the classpath, an ObjectMapper bean is automatically configured.
JsonMapper. Jackson 2 and ObjectMapper support is maintained mainly to help applications migrate.For Jackson 2, Spring Boot provides spring.jackson2.* configuration properties.
For more control, define one or more:
Jackson2ObjectMapperBuilderCustomizer
beans.
If both Jackson 3 and Jackson 2 are present, certain Spring Boot properties can specify that Jackson 2 should be preferred for a particular technology.
| Property | Area |
|---|---|
spring.graphql.rsocket.preferred-json-mapper | GraphQL RSocket |
spring.http.codecs.preferred-json-mapper | WebFlux and reactive HTTP clients |
spring.http.converters.preferred-json-mapper | Spring MVC and imperative HTTP clients |
spring.rsocket.preferred-mapper | RSocket |
spring.websocket.messaging.preferred-json-mapper | WebSocket messaging |
For these properties, set the value to:
jackson2
when you explicitly want Jackson 2 to be preferred for that integration.
Spring Boot also provides auto-configuration for Gson.
When Gson is on the classpath, a Gson bean is automatically configured.
Configuration properties use:
spring.gson.*
For programmatic customization, Spring Boot supports:
GsonBuilderCustomizer
beans.
Spring Boot provides auto-configuration for JSON-B.
When the JSON-B API and an implementation are on the classpath, a Jsonb bean is automatically configured.
The preferred JSON-B implementation in the Spring Boot documentation is Eclipse Yasson, for which dependency management is provided.
JSON-B API
+
JSON-B implementation
↓
Spring Boot auto-configuration
↓
Jsonb bean
Spring Boot also supports Kotlin Serialization.
When:
kotlinx-serialization-json
is on the classpath, Spring Boot automatically configures a Kotlin Serialization Json bean.
Configuration properties use:
spring.kotlinx.serialization.json.*
| Library | Auto-configured object | Current Boot 4.1.1 position |
|---|---|---|
| Jackson 3 | JsonMapper | Preferred/default. |
| Jackson 2 | ObjectMapper | Deprecated migration support. |
| Gson | Gson | Supported. |
| JSON-B | Jsonb | Supported. |
| Kotlin Serialization | Json | Supported. |
Your application
↓
JSON mapping library
↓
Spring Boot auto-configuration
↓
Mapper bean
↓
Serialization / Deserialization
Then choose customization only when the default mapping is not enough.
@JacksonComponent follows component scanning. The class must be in a scanned package.@JacksonComponent for custom Jackson 3 serializers/deserializers that should be Spring-managed.Jackson 3.
Q2. What bean does Spring Boot auto-configure for Jackson 3?A JsonMapper bean.
Converting an application object/value into JSON.
Q4. What is deserialization?Converting JSON into an application object/value.
Q5. What is @JacksonComponent?A Spring Boot annotation that makes it convenient to register Jackson serializers, deserializers or key deserializers as Spring beans.
Q6. What is a Jackson mixin?A mechanism for applying additional Jackson annotations to a target class without modifying that target class directly.
Q7. How does Spring Boot register @JacksonMixin classes?Its Jackson auto-configuration scans application packages and registers them through JacksonMixinModule.
No. Jackson 3 is preferred and default; Jackson 2 support is deprecated.
Q9. How do you customize Jackson 2 more deeply?Use Jackson2ObjectMapperBuilderCustomizer beans.
Use spring.gson.* properties or GsonBuilderCustomizer beans.
Spring Boot automatically configures a Jsonb bean.
When kotlinx-serialization-json is present, Spring Boot auto-configures a Kotlin Serialization Json bean.
@JacksonComponent.ObjectValueSerializer.JsonMapper and Jackson 2 ObjectMapper.preferred-json-mapper properties when both Jackson versions are present.| Goal | Remember |
|---|---|
| Preferred JSON library | Jackson 3 |
| Jackson 3 bean | JsonMapper |
| Jackson 2 bean | ObjectMapper — deprecated support |
| Custom Jackson 3 component | @JacksonComponent |
| Serializer base | ValueSerializer |
| Deserializer base | ValueDeserializer |
| Alternative bases | ObjectValueSerializer, ObjectValueDeserializer |
| Jackson mixin | @JacksonMixin |
| Jackson 2 config | spring.jackson2.* |
| Jackson 2 customizer | Jackson2ObjectMapperBuilderCustomizer |
| Gson config | spring.gson.* |
| Gson customizer | GsonBuilderCustomizer |
| JSON-B bean | Jsonb |
| Kotlin Serialization dependency | kotlinx-serialization-json |
| Kotlin JSON bean | Json |
The most important current Spring Boot 4.1.1 JSON facts are:
JsonMapper.@JacksonComponent makes custom Jackson serializers/deserializers easy to register as Spring beans.@JacksonMixin lets Boot discover and register Jackson mixins.Java Object
↕
JSON Mapper
↕
JSON
Current preferred choice:
Jackson 3 → JsonMapper