HttpMessageConverter in Spring — Serialization and Deserialization
Fast recall
Quick revision
Core answer
Understanding of how @RequestBody and @ResponseBody actually work under the hood
Thinking Jackson just works magically without understanding the MessageConverter chain and content negotiation
Explain content negotiation — Spring selects the converter based on Accept and Content-Type headers, trying each registered converter in order
Interview-ready guide
Interview answer
For example, a POST with Content-Type application/json can be read by MappingJackson2HttpMessageConverter into CreateOrderRequest. Returning OrderResponse can be written by the same converter as application/json when the client accepts it. A plain byte array, Resource, String, or form body may use a different converter.
HttpMessageConverter in Spring — Serialization and Deserialization
Direct Answer
HttpMessageConverter reads a supported HTTP body into a controller argument and writes a return value to a response body. Spring selects a converter from the Java type, request Content-Type, response Accept choices, declared consumes/produces conditions, and registered converter capabilities. @RequestBody and @ResponseBody participate; methods with no body do not necessarily use a converter.
Code Example
@RestController
@RequestMapping("/api/orders")
public class OrderController {
@Autowired
private OrderService orderService;
@PostMapping
public OrderResponse createOrder(@RequestBody OrderRequest request) {
// MappingJackson2HttpMessageConverter deserializes JSON → OrderRequest
var order = orderService.create(request.userId(), request.items());
// Same converter serializes OrderResponse → JSON response body
return new OrderResponse(order.id(), order.total(), "CREATED");
}
}
record OrderRequest(Long userId, List<String> items) {}
record OrderResponse(Long orderId, Double total, String status) {}
// When client sends: POST /api/orders with Content-Type: application/json
// Converter reads JSON bytes → OrderRequest object
// When method returns OrderResponse, converter writes object → JSON bytes
Common Mistakes & Pitfalls
- Forgetting Jackson dependency in non-Boot projects: Freshers expect JSON to work without adding Jackson to pom.xml. Spring Boot includes it by default, but plain Spring MVC doesn't. Without a JSON converter, you get 406 Not Acceptable errors.
- Not understanding Content-Type vs Accept headers: They send JSON but forget Content-Type: application/json header, so Spring can't find a converter to read the request body. Or they expect JSON response but the Accept header says text/html, so Spring picks StringHttpMessageConverter instead.
- Returning raw List from @RestController: Returning List<User> works because MappingJackson2HttpMessageConverter handles collections, but freshers think they need to manually convert it to JSON string first. Never call ObjectMapper yourself in a controller — the converter does it.
- Assuming XML support exists by default: Jackson handles JSON automatically in Boot, but XML requires adding jackson-dataformat-xml dependency. Without it, clients requesting application/xml get 406 errors.
Check your understanding
Answer these without looking back at the article.
- How do you customize Jackson ObjectMapper in Spring Boot?
- How does Spring select which HttpMessageConverter to use?
- What is the difference between @ResponseBody and HttpMessageConverter?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.