How to Use Java Jackson for JSON Serialization and Deserialization in Spring Boot
Add the spring-boot-starter-json dependency to your build file; it transitively includes jackson-databind and automatically configures the ObjectMapper bean and HTTP message converters for seamless JSON handling.
To enable java jackson for JSON serialization and deserialization in a Spring Boot application, you only need a single starter dependency. The spring-projects/spring-boot repository provides a curated set of Jackson libraries through the spring-boot-starter-json module, eliminating the need to manage individual Jackson versions or compatibility manually.
The Essential Dependency: spring-boot-starter-json
The spring-boot-starter-json artifact is the only dependency you need to add for full Jackson support. According to the source code in starter/spring-boot-starter-json/build.gradle, this starter pulls in the spring-boot-jackson module, which acts as the bridge to the actual Jackson libraries.
The spring-boot-jackson module, defined in module/spring-boot-jackson/build.gradle, declares the core jackson-databind library along with optional format modules such as jackson-dataformat-xml and jackson-dataformat-cbor. This means you get com.fasterxml.jackson.core:jackson-databind on your classpath without explicitly declaring it.
Maven and Gradle Configuration
You can declare the dependency in your build file using the coordinates below. Note that if you already use spring-boot-starter-web or spring-boot-starter-webflux, the JSON starter is included automatically, as shown in starter/spring-boot-starter-web/build.gradle.
Maven Coordinates
<!-- Minimal Jackson support -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-json</artifactId>
</dependency>
<!-- Already covered by spring-boot-starter-web (includes the above) -->
<!--
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
-->
Gradle Configuration
// Minimal Jackson support
implementation 'org.springframework.boot:spring-boot-starter-json'
// If you use Spring MVC or WebFlux you already have it via the web starter
// implementation 'org.springframework.boot:spring-boot-starter-web'
// implementation 'org.springframework.boot:spring-boot-starter-webflux'
The versions for all Jackson libraries are managed by the Spring Boot dependency management BOM defined in platform/spring-boot-dependencies/build.gradle, ensuring compatibility across your project.
How Spring Boot Auto-Configures Jackson
When spring-boot-starter-json is present on the classpath, Spring Boot automatically registers two critical components:
ObjectMapperbean: The core Jackson class for serialization and deserialization is configured as a Spring bean with sensible defaults (e.g.,WRITE_DATES_AS_TIMESTAMPSdisabled).MappingJackson2HttpMessageConverter: This HTTP message converter is registered to handleapplication/jsoncontent types, automatically converting request bodies to Java objects and responses to JSON.
This auto-configuration happens through the JacksonAutoConfiguration class, which is triggered by the presence of the Jackson libraries on the classpath provided by the starter.
Practical Code Examples
Basic POJO and REST Controller
The following example demonstrates automatic JSON handling in a Spring Boot application. Create a simple POJO and a REST controller:
// src/main/java/com/example/demo/User.java
package com.example.demo;
public class User {
private String name;
private int age;
// getters & setters
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public int getAge() { return age; }
public void setAge(int age) { this.age = age; }
}
// src/main/java/com/example/demo/UserController.java
package com.example.demo;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/users")
public class UserController {
@PostMapping
public User echo(@RequestBody User user) {
// Jackson deserialises JSON → User, then serialises User → JSON
return user;
}
}
Test the endpoint with curl:
curl -X POST http://localhost:8080/users \
-H "Content-Type: application/json" \
-d '{"name":"Alice","age":30}'
The response will be {"name":"Alice","age":30}, confirming that Jackson is handling both deserialization (request body to User object) and serialization (User object to response body).
Direct ObjectMapper Usage
For manual JSON processing outside of HTTP endpoints, inject the pre-configured ObjectMapper bean:
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.stereotype.Component;
@Component
public class JsonUtil {
private final ObjectMapper mapper;
public JsonUtil(ObjectMapper mapper) {
this.mapper = mapper; // Spring injects the configured mapper
}
public String toJson(Object obj) throws Exception {
return mapper.writeValueAsString(obj);
}
public <T> T fromJson(String json, Class<T> type) throws Exception {
return mapper.readValue(json, type);
}
}
Because the ObjectMapper is supplied by spring-boot-starter-json, it inherits all Spring Boot’s default configurations (such as date formatting and property naming strategies).
Summary
- Use
spring-boot-starter-jsonas the single dependency to enable java jackson for JSON serialization and deserialization in Spring Boot projects. - The starter transitively includes
jackson-databindand optional format modules, with versions managed by the Spring Boot BOM inplatform/spring-boot-dependencies/build.gradle. - If you use
spring-boot-starter-weborspring-boot-starter-webflux, the JSON starter is already included and requires no additional declaration. - Spring Boot auto-configures an
ObjectMapperbean and registersMappingJackson2HttpMessageConverterto handle JSON conversion automatically for REST endpoints.
Frequently Asked Questions
Do I need to add Jackson dependencies manually if I use spring-boot-starter-web?
No. The spring-boot-starter-web dependency transitively includes spring-boot-starter-json, as defined in starter/spring-boot-starter-web/build.gradle. This means Jackson is already available on your classpath for JSON serialization and deserialization without any additional configuration.
What is the difference between spring-boot-starter-json and jackson-databind?
spring-boot-starter-json is a Spring Boot-specific starter that aggregates spring-boot-jackson, which in turn declares jackson-databind (the core Jackson library) along with optional modules for XML and CBOR formats. While you could declare jackson-databind directly, using the starter ensures version compatibility with Spring Boot’s dependency management and enables auto-configuration of the ObjectMapper bean.
How do I customize the ObjectMapper configuration in Spring Boot?
You can customize the auto-configured ObjectMapper by defining a Jackson2ObjectMapperBuilderCustomizer bean or by setting properties in your application.properties or application.yml file (such as spring.jackson.date-format or spring.jackson.property-naming-strategy). Alternatively, you can create your own ObjectMapper bean marked with @Primary to replace the auto-configured instance entirely.
Where are the Jackson dependency versions defined in the Spring Boot repository?
Jackson library versions are managed centrally in platform/spring-boot-dependencies/build.gradle, which imports the Jackson BOM (Bill of Materials). This ensures that all Jackson modules (core, databind, annotations, and dataformat extensions) use compatible versions without requiring you to specify version numbers in your build file.
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →