Jackson JSON Serialization in Spring Boot
Fast recall
Quick revision
Core answer
HttpMessageConverter. When a Jackson converter is selected, the configured Jackson mapper deserializes JSON into an @RequestBody DTO and serializes a value returned by a @RestController into JSON. DTO design, mapper settings, validation, and error handling complete the contract.Interview-ready guide
Interview answer
Spring MVC does not ask a controller to parse JSON by hand. A parameter such as @RequestBody CreateUserRequest request tells Spring to read the HTTP body through an HttpMessageConverter. When the request has Content-Type: application/json and a Jackson converter is configured, that converter uses the application's Jackson mapper to build the declared Java type.
During deserialization, Jackson reads JSON members and fills the request DTO according to its names, types, configuration, and annotations. For example, @JsonProperty("display_name") String displayName maps the JSON member display_name to the Java component displayName. If the controller parameter also has @Valid, Bean Validation runs after successful JSON conversion.
The reverse path starts when a @RestController returns a value such as new UserResponse(42, "Ada"). Spring selects a converter for the response media type, and Jackson serializes the DTO into JSON. Accept and a mapping's produces condition can take part in response selection; the response Content-Type tells the client which format was actually sent.
Shared rules such as property naming, time formats, modules, and unknown-member policy should be configured centrally so every endpoint behaves the same. A focused annotation such as @JsonProperty is useful for one contract name. Request and response DTOs keep the public shape deliberate; directly serializing persistence entities can expose internal fields, cycles, or lazy-loaded relationships.
Malformed JSON or an incompatible value fails during message conversion, before the controller receives a DTO. Successful deserialization does not prove the request is acceptable: Bean Validation and service rules still run afterward. Serialization can fail later if a custom mapper rule is broken. Each stage needs a safe, consistent error response, and strict compatibility rules should be tested against the mapper actually configured by the application.
Jackson JSON Serialization in Spring Boot
Direct Answer
Spring MVC reads and writes HTTP bodies through an HttpMessageConverter. When a Jackson converter is selected, the configured Jackson mapper deserializes JSON into an @RequestBody DTO and serializes a value returned by a @RestController into JSON. DTO design, mapper settings, validation, and error handling complete the contract.
Deep Dive & Architecture
Handler mapping first selects a controller method from its path, HTTP method, and media conditions. Argument resolution sees @RequestBody and delegates to a converter that can read the request's Content-Type into the declared target type. The Jackson converter parses tokens, constructs the object, converts property values, and applies the configured mapping rules.
The reverse path begins with the controller's return value. Spring chooses a converter for the negotiated response media type, and Jackson serializes the response DTO. Request and response DTOs define a deliberate public shape. Global naming or time conventions can be configured centrally, while one-off contract names can use focused annotations or a custom serializer when normal mapping is not enough.
Malformed JSON fails during message conversion before controller logic receives a DTO. Valid JSON can still violate Bean Validation or domain rules afterward. Serialization can also fail, for example when a custom serializer throws. These stages should map to safe, consistent error responses rather than expose parser or mapping internals.
Deserialization is a trust boundary. Unknown-member handling, polymorphic input, numeric coercion, null behavior, and accepted date formats should be intentional. Mapper types and defaults can differ between Spring Boot and Jackson generations, so a compatibility-sensitive API should configure and test the actual mapper used by that application instead of relying on a remembered default.
Code Example
import com.fasterxml.jackson.annotation.JsonProperty;
import org.springframework.web.bind.annotation.*;
record CreateUserRequest(
@JsonProperty("display_name") String displayName,
String email) {}
record UserResponse(long id, String displayName) {}
@RestController
@RequestMapping("/users")
final class UserController {
@PostMapping
UserResponse create(@RequestBody CreateUserRequest request) {
return new UserResponse(42, request.displayName());
}
}
With Jackson and Spring MVC configured, the request member display_name maps to the Java component displayName, and the returned record is serialized as a JSON response.
Check your understanding
Answer these without looking back at the article.
- What role does an `HttpMessageConverter` play in Spring MVC?
- When should Jackson configuration be global rather than annotation-based?
- At what stage does malformed JSON fail?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.