Spring Boot JSON

Complete beginner-friendly chapter — Jackson 3, custom serializers/deserializers, mixins, Gson, JSON-B, Kotlin Serialization and JSON mapper selection
Source basis: This chapter follows the current Spring Boot 4.1.1 official JSON documentation. The current documentation lists Jackson 3, Jackson 2, Gson, JSON-B and Kotlin Serialization, with Jackson 3 as the preferred and default library.

1. The Big Idea

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.

Java object → JSON = SerializationJSON → Java object = Deserialization

2. Spring Boot JSON Support

Spring Boot provides integration with several JSON mapping libraries:

LibraryCurrent Boot 4.1.1 status
Jackson 3Preferred and default.
Jackson 2Deprecated; provided mainly to ease migration to Jackson 3.
GsonSupported through auto-configuration.
JSON-BSupported through auto-configuration.
Kotlin SerializationSupported through auto-configuration.
Important current-version point: Jackson 2 support is deprecated in Spring Boot 4.x and is intended to be removed in a future Spring Boot 4.x release. For new work, Jackson 3 is the preferred choice.

3. What Is JSON Mapping?

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.

Interview point: In current Spring Boot 4.1.1, think Jackson 3 → JsonMapper, not the older Jackson 2 ObjectMapper as the default.

4. Jackson 3 — The Preferred Library

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
Simple idea: You normally do not need to manually create a JsonMapper just to start working with JSON in Spring Boot.

5. Serialization

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.

6. Deserialization

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.

7. Custom Serializers and Deserializers

Sometimes the default mapping is not exactly what your application needs.

For example, you may want:

  • a custom JSON field name,
  • a special representation of a value,
  • custom formatting,
  • special parsing rules.

Spring Boot's current Jackson 3 documentation provides ValueSerializer and ValueDeserializer as the relevant extension types.

8. @JacksonComponent

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 implementations
  • ValueDeserializer implementations
  • KeyDeserializer implementations
  • a class containing serializer/deserializer inner classes
Easy memory: @JacksonComponent = “Register my Jackson customization as a Spring bean.”

9. How @JacksonComponent Works

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

10. Custom Serializer Example

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.

11. Custom Deserializer Example

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.

12. ObjectValueSerializer and ObjectValueDeserializer

Spring Boot also provides:

ObjectValueSerializerObjectValueDeserializer

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);
    }
}

13. Why Custom Serialization Is Useful

SituationWhy customize?
Legacy JSON formatMatch an existing external API.
Special field representationControl exactly what JSON contains.
Custom deserializationConvert unusual JSON structures into domain objects.
API compatibilityKeep your Java model independent from an external JSON contract.

14. Jackson Mixins

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:

  • the target class belongs to a third-party library,
  • you cannot modify the original class,
  • you want JSON-specific annotations outside the domain class.

15. @JacksonMixin

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.

Memory: @JacksonMixin = “Apply Jackson annotations to another class without changing that class directly.”

16. Jackson 2 — Current Status

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.

Do not confuse versions: The current default is Jackson 3 and JsonMapper. Jackson 2 and ObjectMapper support is maintained mainly to help applications migrate.

17. Jackson 2 Customization

For Jackson 2, Spring Boot provides spring.jackson2.* configuration properties.

For more control, define one or more:

Jackson2ObjectMapperBuilderCustomizer

beans.

18. When Both Jackson 3 and Jackson 2 Exist

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.

PropertyArea
spring.graphql.rsocket.preferred-json-mapperGraphQL RSocket
spring.http.codecs.preferred-json-mapperWebFlux and reactive HTTP clients
spring.http.converters.preferred-json-mapperSpring MVC and imperative HTTP clients
spring.rsocket.preferred-mapperRSocket
spring.websocket.messaging.preferred-json-mapperWebSocket messaging

For these properties, set the value to:

jackson2

when you explicitly want Jackson 2 to be preferred for that integration.

19. Gson

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.

20. JSON-B

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

21. Kotlin Serialization

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.*

22. JSON Library Comparison

LibraryAuto-configured objectCurrent Boot 4.1.1 position
Jackson 3JsonMapperPreferred/default.
Jackson 2ObjectMapperDeprecated migration support.
GsonGsonSupported.
JSON-BJsonbSupported.
Kotlin SerializationJsonSupported.

23. The Spring Boot JSON Mental Model

Your application
      ↓
JSON mapping library
      ↓
Spring Boot auto-configuration
      ↓
Mapper bean
      ↓
Serialization / Deserialization

Then choose customization only when the default mapping is not enough.

Default mappingConfiguration propertiesCustomizersCustom serializersMixins

24. Common Mistakes

  1. Assuming Jackson 2 is still the current default. Spring Boot 4.1.1 prefers Jackson 3.
  2. Using old Jackson 2 APIs in a new Jackson 3 setup. Check the package/API version carefully.
  3. Creating a custom mapper unnecessarily. Boot already provides auto-configuration.
  4. Putting JSON-specific concerns everywhere in domain classes. Consider custom serializers or mixins when appropriate.
  5. Forgetting that @JacksonComponent follows component scanning. The class must be in a scanned package.
  6. Using Jackson 2 without a migration reason. Current documentation marks Jackson 2 support as deprecated.
  7. Adding a second JSON library without understanding mapper selection. If multiple libraries are present, know which integration is using which mapper.

25. Best Practices

  • For new Spring Boot 4.x applications, prefer Jackson 3 unless you have a specific reason otherwise.
  • Let Spring Boot auto-configure the standard mapper first.
  • Use configuration properties for straightforward customization.
  • Use @JacksonComponent for custom Jackson 3 serializers/deserializers that should be Spring-managed.
  • Use mixins when you need JSON annotations without changing the target class.
  • Keep external JSON contracts stable and intentionally versioned.
  • Do not expose internal domain structure accidentally through JSON APIs.
  • When migrating from Jackson 2, explicitly identify the integrations that still require Jackson 2.

26. Interview Questions

Q1. Which JSON library is preferred in Spring Boot 4.1.1?

Jackson 3.

Q2. What bean does Spring Boot auto-configure for Jackson 3?

A JsonMapper bean.

Q3. What is serialization?

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.

Q8. Is Jackson 2 the default in current Spring Boot 4.1.1?

No. Jackson 3 is preferred and default; Jackson 2 support is deprecated.

Q9. How do you customize Jackson 2 more deeply?

Use Jackson2ObjectMapperBuilderCustomizer beans.

Q10. How do you customize Gson?

Use spring.gson.* properties or GsonBuilderCustomizer beans.

Q11. What happens when JSON-B API and an implementation are on the classpath?

Spring Boot automatically configures a Jsonb bean.

Q12. What does Kotlin Serialization use?

When kotlinx-serialization-json is present, Spring Boot auto-configures a Kotlin Serialization Json bean.

27. Practice Exercises

  1. Create a simple Spring Boot REST endpoint that returns a Java object.
  2. Observe the JSON representation generated by the default mapper.
  3. Create a custom Jackson 3 serializer using @JacksonComponent.
  4. Create a custom deserializer for the same object.
  5. Rewrite the serializer using ObjectValueSerializer.
  6. Create a Jackson mixin for a class you cannot modify.
  7. Configure Gson and inspect its auto-configured bean.
  8. Explain the difference between Jackson 3 JsonMapper and Jackson 2 ObjectMapper.
  9. Research which application integrations would use the preferred-json-mapper properties when both Jackson versions are present.
  10. Design a migration plan from Jackson 2 to Jackson 3.

28. Quick Cheat Sheet

GoalRemember
Preferred JSON libraryJackson 3
Jackson 3 beanJsonMapper
Jackson 2 beanObjectMapper — deprecated support
Custom Jackson 3 component@JacksonComponent
Serializer baseValueSerializer
Deserializer baseValueDeserializer
Alternative basesObjectValueSerializer, ObjectValueDeserializer
Jackson mixin@JacksonMixin
Jackson 2 configspring.jackson2.*
Jackson 2 customizerJackson2ObjectMapperBuilderCustomizer
Gson configspring.gson.*
Gson customizerGsonBuilderCustomizer
JSON-B beanJsonb
Kotlin Serialization dependencykotlinx-serialization-json
Kotlin JSON beanJson

29. Memory Map

Spring Boot JSON →

JSON → Serialization / Deserialization → Jackson 3 default → JsonMapper → Custom serializers/deserializers → @JacksonComponent → Mixins → Jackson 2 migration support → Gson → JSON-B → Kotlin Serialization

30. Final Takeaway

The most important current Spring Boot 4.1.1 JSON facts are:

  • Spring Boot supports multiple JSON mapping libraries.
  • Jackson 3 is the preferred and default library.
  • When Jackson 3 is available, Boot auto-configures a JsonMapper.
  • @JacksonComponent makes custom Jackson serializers/deserializers easy to register as Spring beans.
  • @JacksonMixin lets Boot discover and register Jackson mixins.
  • Jackson 2 is deprecated and mainly supported to help applications migrate.
  • Gson, JSON-B and Kotlin Serialization also have Boot auto-configuration.
Java Object
     ↕
JSON Mapper
     ↕
   JSON

Current preferred choice:
Jackson 3 → JsonMapper