Hackforge Academy

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";
}

πŸš€ Learn Spring Boot with real-world projects

πŸ’‘ Build REST APIs step by step

🧠 Improve backend development skills

🎯 Get career-ready practical training

Join Our Free WhatsApp Community

Direct access to niche-specific mentors and peers on WhatsApp.

🐍

Python Community

Discuss Django, FastAPI, AI integration, and automation scripts with 15k+ developers.

Join Python Community
βš›οΈ

React Community

Master Next.js, Framer Motion, and State Management. Share your latest UI components.

Join React Community
β˜•

Java Community

Deep dives into Spring Boot, Microservices architecture, and high-performance backend ops.

Join Java Community