@RequestBody in Spring REST — JSON to Java Object Deserialization
Fast recall
Quick revision
Core answer
Core request-body annotation; use it when the endpoint actually accepts a converted HTTP body
Forgetting to send Content-Type: application/json header when calling the API — causes 415 Unsupported Media Type
Mention @Valid with @RequestBody for automatic Bean Validation
Interview-ready guide
Interview answer
For @PostMapping(consumes = application/json), a JSON request can be converted into CreateOrderRequest through the Jackson converter. Spring does not literally promise to call ObjectMapper.readValue in one particular way; the converter owns that implementation. A text/plain endpoint can bind the body to String without Jackson. If the client sends XML to a JSON-only endpoint, handler/converter selection fails rather than silently treating it as JSON.
@RequestBody in Spring REST — JSON to Java Object Deserialization
Direct Answer
@RequestBody tells Spring MVC to read the HTTP request body into a method parameter using an HttpMessageConverter. JSON with Jackson is common, but the annotation is not JSON-only: text, XML, bytes, forms, and custom media types can use other converters. Content-Type identifies the representation sent by the client.
Code Example
@RestController
@RequestMapping("/api/users")
public class UserController {
@PostMapping
public ResponseEntity<User> createUser(@RequestBody User user) {
// user object is already populated from JSON request body
var saved = userService.save(user);
return ResponseEntity.status(HttpStatus.CREATED).body(saved);
}
}
record User(String name, String email, int age) {}
// Client sends: POST /api/users with body {"name":"Alice","email":"alice@example.com","age":25}
// Spring deserializes JSON into User record automatically
// If Content-Type header is missing, you get 415 Unsupported Media Type
Common Mistakes & Pitfalls
- Missing Content-Type header: Freshers test with Postman or curl but forget to set Content-Type: application/json. Spring returns 415 Unsupported Media Type because it doesn't know the body is JSON. Always set this header when sending JSON.
- Using @RequestBody with GET: GET requests have no body by design. Freshers sometimes add @RequestBody to GET methods, which doesn't work — use @RequestParam or @PathVariable instead.
- Not handling deserialization failures: If the JSON structure doesn't match the Java class (wrong field names, type mismatches), Jackson throws an exception. Freshers don't catch this — the client sees a 400 error with no useful message. Use @ControllerAdvice to handle HttpMessageNotReadableException and return a clear error response.
Check your understanding
Answer these without looking back at the article.
- What happens if the client doesn't set Content-Type: application/json with @RequestBody?
- How do you validate a @RequestBody in Spring Boot?
- What is the difference between @Valid and @Validated?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.