JSON (JavaScript Object Notation) is the most widely used data format in REST APIs. Spring Boot, by default, uses Jackson as its JSON library to serialize Java objects to JSON and deserialize JSON into Java objects.
In this post, weβll explore how Jackson works with Spring Boot, how to customize the JSON behavior, and best practices for working with JSON in your application.

π§ Jackson in Spring Boot
Spring Boot automatically includes Jackson dependencies through spring-boot-starter-web. This makes Jackson the default JSON processor.
β¨ Key features of Jackson:
- Object to JSON and JSON to Object conversion
- Annotations for custom mappings
- Support for nested objects, collections, maps, etc.
β Basic Setup
1. Add Spring Web Dependency
If you’re using Maven:
1 2 3 4 5 |
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> |
Jackson is included transitively in spring-boot-starter-web
.
π§ͺ JSON Input Example: POST API
Letβs create a simple REST API that accepts a JSON payload.
π Model Class: User.java
1 2 3 4 5 6 7 8 9 10 11 12 13 |
package com.kscodes.springboot.jsondemo.model; public class User { private String name; private int age; private String email; // Getters and setters // toString() for logging } |
π Controller: UserController.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
package com.kscodes.springboot.jsondemo.controller; import com.kscodes.springboot.jsondemo.model.User; import org.springframework.web.bind.annotation.*; @RestController @RequestMapping("/api/users") public class UserController { @PostMapping public String createUser(@RequestBody User user) { // Jackson converts JSON to User object return "User created: " + user.toString(); } } |
π§Ύ Sample JSON Input
1 2 3 4 5 6 7 8 |
{ "name": "John Doe", "age": 30, "email": "john@example.com" } |
π€ JSON Output Example: GET API
You can also return Java objects as JSON.
π Extend the Controller
1 2 3 4 5 6 7 8 9 10 11 |
@GetMapping("/{name}") public User getUser(@PathVariable String name) { // Simulate user data User user = new User(); user.setName(name); user.setAge(25); user.setEmail(name.toLowerCase() + "@example.com"); return user; } |
Spring Boot automatically converts the User
object to JSON using Jackson.
π οΈ Customizing JSON with Jackson Annotations
1. @JsonProperty
β Change JSON property name
1 2 3 4 5 |
@JsonProperty("user_name") private String name; |
2. @JsonIgnore
β Ignore field in JSON
1 2 3 4 5 |
@JsonIgnore private String password; |
3. @JsonInclude
β Exclude null
or default values
1 2 3 4 5 |
@JsonInclude(JsonInclude.Include.NON_NULL) private String email; |
π¦ Handling Nested JSON
1 2 3 4 5 6 7 8 9 10 |
public class Address { private String city; private String country; } public class User { private String name; private Address address; } |
Jackson will automatically map nested JSON like:
1 2 3 4 5 6 7 8 9 10 |
{ "name": "Alice", "address": { "city": "Mumbai", "country": "India" } } |
π Convert Objects Manually (Optional)
Sometimes you may need to convert manually.
Inject ObjectMapper
1 2 3 4 5 |
@Autowired private ObjectMapper objectMapper; |
Convert Object to JSON
1 2 3 4 |
String json = objectMapper.writeValueAsString(user); |
Convert JSON to Object
1 2 3 4 |
User user = objectMapper.readValue(json, User.class); |
π§ͺ Testing the APIs with Postman or curl
POST
1 2 3 4 5 6 |
curl -X POST http://localhost:8080/api/users \ -H "Content-Type: application/json" \ -d '{"name":"Jane","age":28,"email":"jane@example.com"}' |
GET
1 2 3 4 |
curl http://localhost:8080/api/users/Jane |
βοΈ Customize ObjectMapper Globally
You can define a custom ObjectMapper
bean to control serialization globally.
1 2 3 4 5 6 7 8 9 10 |
@Bean public ObjectMapper objectMapper() { ObjectMapper mapper = new ObjectMapper(); mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); mapper.enable(SerializationFeature.INDENT_OUTPUT); return mapper; } |
π§° Common Pitfalls
Issue | Solution |
---|---|
JSON parse error | Check if @RequestBody is used and all fields match |
Null values in response | Use @JsonInclude(Include.NON_NULL) |
Extra fields ignored | Enable mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false) |
π External References
π Conclusion
Spring Boot and Jackson provide an out-of-the-box solution for JSON handling. You can start building JSON APIs with almost no configuration. For advanced needs, Jackson annotations and ObjectMapper
customization give you full control over serialization and deserialization.