Category: java
RequestBody is used in Spring Boot
Published on 11 Apr 2026
Explanation
@RequestBody is used in Spring Boot to
map the HTTP request body (usually JSON
data) to a Java object automatically. It
is commonly used in POST and PUT
APIs when the client sends structured data.
Code:
@PostMapping("/user")
public String createUser(@RequestBody
User user) {
return "User received: " +
user.getName();
}
Explanation
Spring Boot automatically converts
incoming JSON into
a Java object using Jackson library when
@RequestBody is applied to a method
parameter.
Code:
public class User {
private String name;
private int age;
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; }
}
Explanation
Example JSON request sent from client that
will be mapped to the User object
using @RequestBody.
Code:
{
"name": "Praveen",
"age": 25
}
Explanation
@RequestBody is mainly used when data is
sent in the request body instead of
URL parameters. It helps build REST APIs
that accept JSON input from frontend apps
like React or Angular. π
Code:
@PutMapping("/updateUser")
public String updateUser(
@RequestBody User user)
{
return "Updated user: " +
user.getName();
}
Explanation
You can also validate request data using
@Valid with @RequestBody to ensure
input correctness
before processing.
Code:
@PostMapping("/save")
public String saveUser(@Valid @RequestBody
User user) {
return "User saved successfully";
}