How to Get Request Body in Spring Boot: Simple Guide
In Spring Boot, you get the request body by adding the
@RequestBody annotation to a method parameter in your controller. This tells Spring to convert the incoming JSON or other data into a Java object automatically.Syntax
Use the @RequestBody annotation on a controller method parameter to bind the HTTP request body to a Java object.
@RequestBody: Tells Spring to read the request body and convert it.- Method parameter: The Java class matching the expected request data.
- Controller method: Handles the HTTP request.
java
public ResponseEntity<String> exampleMethod(@RequestBody MyData data) { // use data object }
Example
This example shows a Spring Boot controller that receives JSON data in the request body and maps it to a Java object using @RequestBody. It then returns a confirmation message.
java
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; @RestController public class MyController { public static class MyData { private String name; private int age; // Getters and setters public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } } @PostMapping("/submit") public ResponseEntity<String> receiveData(@RequestBody MyData data) { String response = "Received name: " + data.getName() + ", age: " + data.getAge(); return ResponseEntity.ok(response); } }
Output
HTTP 200 OK
Body: Received name: Alice, age: 30
Common Pitfalls
- Forgetting
@RequestBodycauses Spring to look for request parameters instead of the body. - Not providing getters and setters in the data class prevents Spring from mapping JSON properly.
- Sending invalid JSON or mismatched fields causes errors or null values.
- Using
@RequestParamor@PathVariableinstead of@RequestBodyfor body data is incorrect.
java
/* Wrong: Missing @RequestBody */ @PostMapping("/wrong") public ResponseEntity<String> wrongMethod(MyData data) { // data will be null or empty return ResponseEntity.ok("Got data"); } /* Right: With @RequestBody */ @PostMapping("/right") public ResponseEntity<String> rightMethod(@RequestBody MyData data) { return ResponseEntity.ok("Got name: " + data.getName()); }
Quick Reference
Remember these tips when getting request body in Spring Boot:
- Use
@RequestBodyon the parameter to bind the body. - Ensure your data class has public getters and setters.
- Send valid JSON matching your data class fields.
- Use
@PostMappingor other HTTP method annotations on the controller method.
Key Takeaways
Use @RequestBody annotation on a controller method parameter to get the request body.
The request body is automatically converted to a Java object if the class has proper getters and setters.
Always send valid JSON matching your Java class fields to avoid mapping errors.
Forgetting @RequestBody or missing getters/setters are common mistakes causing null or empty data.
Use appropriate HTTP method annotations like @PostMapping to handle requests with bodies.