@RequestBody helps your Spring Boot app read JSON data sent by a client and turn it into Java objects automatically.
@RequestBody for JSON input in Spring Boot
@PostMapping("/path")
public ResponseEntity<Type> methodName(@RequestBody Type variableName) {
// use variableName here
}The @RequestBody annotation tells Spring to convert JSON from the request body into the Java object.
The Java class used must have getters/setters or be a record for Spring to map JSON fields properly.
@PostMapping("/users") public ResponseEntity<User> addUser(@RequestBody User user) { // save user return ResponseEntity.ok(user); }
@PutMapping("/products/{id}") public ResponseEntity<Product> updateProduct(@PathVariable int id, @RequestBody Product product) { // update product with id return ResponseEntity.ok(product); }
This Spring Boot app has a POST endpoint at /users that accepts JSON like {"name":"Alice","age":30}.
Spring converts the JSON into a User record automatically using @RequestBody.
The endpoint returns the same user data as confirmation.
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RestController; @SpringBootApplication public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } } record User(String name, int age) {} @RestController class UserController { @PostMapping("/users") public ResponseEntity<User> addUser(@RequestBody User user) { // Just return the user received return ResponseEntity.ok(user); } }
Make sure your JSON keys match the Java object's field names exactly for mapping to work.
If JSON is invalid or missing required fields, Spring will return a 400 Bad Request error automatically.
Use records or classes with public getters/setters for smooth JSON to Java conversion.
@RequestBody reads JSON from HTTP requests and converts it to Java objects automatically.
It is useful for REST APIs that accept JSON input from clients.
Spring Boot handles errors if JSON is missing or malformed.
Practice
@RequestBody annotation in a Spring Boot controller?Solution
Step 1: Understand the role of @RequestBody
@RequestBody tells Spring Boot to take JSON data sent in the HTTP request body and convert it into a Java object automatically.Step 2: Differentiate from other annotations
It does not handle response data (that's @ResponseBody), nor does it map query parameters or validate GET form data.Final Answer:
To automatically convert JSON from the HTTP request body into a Java object -> Option DQuick Check:
@RequestBody = JSON to Java object [OK]
- Confusing @RequestBody with @ResponseBody
- Thinking it maps URL parameters
- Assuming it validates data automatically
@RequestBody to accept a JSON object representing a User in a Spring Boot controller?Solution
Step 1: Identify correct annotation for JSON body
@RequestBody is required to tell Spring Boot to convert JSON in the request body to a User object.Step 2: Check other annotations
@RequestParam reads query parameters, @PathVariable reads URL path segments, and no annotation means no automatic JSON conversion.Final Answer:
public ResponseEntity<String> addUser(@RequestBody User user) -> Option AQuick Check:
JSON input needs @RequestBody [OK]
- Using @RequestParam instead of @RequestBody
- Forgetting @RequestBody annotation
- Confusing @PathVariable with JSON input
@PostMapping("/add")
public String addUser(@RequestBody User user) {
return "Hello " + user.getName();
}And the JSON sent in the request body is:
{"name": "Alice"}What will be the response?
Solution
Step 1: Understand JSON to Java object mapping
The JSON {"name": "Alice"} maps to a User object with name set to "Alice".Step 2: Check method return value
The method returns "Hello " + user.getName(), so it returns "Hello Alice".Final Answer:
"Hello Alice" -> Option CQuick Check:
JSON name maps to user.getName() = "Hello Alice" [OK]
- Assuming user.getName() is null
- Expecting HTTP error without malformed JSON
- Confusing request body with query parameters
@PostMapping("/user")
public String createUser(@RequestBody User user) {
return user.getEmail();
}If the client sends an empty request body, what will happen?
Solution
Step 1: Check behavior with empty JSON body
When @RequestBody expects JSON but receives empty body, Spring Boot cannot create the User object.Step 2: Understand Spring Boot error handling
Spring Boot responds with HTTP 400 Bad Request for missing or malformed JSON input.Final Answer:
Spring Boot throws an HTTP 400 Bad Request error -> Option AQuick Check:
Empty JSON body causes HTTP 400 error [OK]
- Assuming method returns null or empty string
- Thinking compilation error occurs
- Ignoring HTTP error on missing JSON
@RequestBody to achieve this?Solution
Step 1: Identify correct parameter type for JSON array
To accept a JSON array of User objects, the method parameter should be a List<User> annotated with @RequestBody.Step 2: Exclude incorrect annotations and types
@RequestParam and @PathVariable do not handle JSON body. Single User parameter cannot accept an array.Final Answer:
public int countUsers(@RequestBody List<User> users) -> Option BQuick Check:
JSON array maps to List<User> with @RequestBody [OK]
- Using single User instead of List<User>
- Using @RequestParam or @PathVariable for JSON body
- Forgetting @RequestBody annotation
Ready to prove it?
Pass the test and this pattern moves unknown
