← Back to Questions
Spring Boot

What is @RequestBody?

Learn What is @RequestBody? with simple explanations, real-time examples, interview tips and practical use cases.

What is @RequestBody in Spring Boot?

@RequestBody is one of the most commonly used annotations in Spring Boot REST APIs.

It is used to:

  • Read HTTP request body data
  • Convert JSON into Java Objects
  • Handle REST API input data

Spring automatically maps incoming JSON data to Java objects using Jackson library.


Simple Definition

@RequestBody is used to convert incoming request body data into Java objects automatically.

@RequestBody = Converts client JSON request into Java object.

Why is @RequestBody Needed?

In REST APIs, frontend applications send data as:

  • JSON
  • XML

Spring Boot needs a way to convert that data into Java objects automatically.

@RequestBody performs this conversion.


Real-Time Example

Suppose we are developing Dhanish Empower.

Frontend sends course creation request:

{
  "courseName": "Spring Boot",
  "price": 999
}

Backend receives and converts it into:

Course course

using:

@RequestBody

Basic Example

@PostMapping("/courses")
public Course saveCourse(
        @RequestBody Course course) {

    return courseService.save(course);
}

Here:

  • Frontend sends JSON
  • Spring converts JSON into Course object
  • Course object is passed to method

Frontend JSON Request

{
  "id": 1,
  "courseName": "Java Full Stack",
  "price": 1999
}

Java Entity Class

public class Course {

    private Long id;
    private String courseName;
    private double price;

}

Spring automatically maps JSON fields to Java object fields.


How @RequestBody Works Internally

  1. Client sends HTTP request with JSON.
  2. Spring receives request body.
  3. Jackson library parses JSON.
  4. JSON fields are mapped to Java object.
  5. Object is injected into controller method.

Internal Flow Diagram

Client JSON Request
        ↓
@RequestBody
        ↓
Jackson JSON Parser
        ↓
Java Object Conversion
        ↓
Controller Method

Common HTTP Methods Using @RequestBody

HTTP Method Usage
POST Create new data
PUT Update existing data
PATCH Partial update

POST Example with @RequestBody

@PostMapping("/users")
public User saveUser(
        @RequestBody User user) {

    return userService.save(user);
}

PUT Example with @RequestBody

@PutMapping("/users/{id}")
public User updateUser(
        @PathVariable Long id,
        @RequestBody User user) {

    return userService.update(id, user);
}

@RequestBody with Validation

We can combine:

@Valid + @RequestBody

to validate incoming request data.

Example

@PostMapping("/courses")
public Course saveCourse(
        @Valid
        @RequestBody Course course) {

    return courseService.save(course);
}

Validation Entity Example

public class Course {

    @NotBlank
    private String courseName;

    @Positive
    private double price;

}

Spring automatically validates request data.


What Happens Without @RequestBody?

Spring cannot properly convert JSON request data into Java object.

Result:

  • Null values
  • 400 Bad Request
  • Binding errors

@RequestBody vs @RequestParam

@RequestBody @RequestParam
Reads JSON request body Reads query parameters
Used for complex objects Used for simple values
Mainly POST/PUT APIs Mainly GET requests

@RequestBody vs @PathVariable

@RequestBody @PathVariable
Reads request body Reads URL values
Used for JSON data Used for IDs and path values

Real-Time Example in Dhanish Empower

@RestController
@RequestMapping("/api/payments")
public class PaymentController {

    @PostMapping("/create")
    public PaymentResponse createPayment(
            @RequestBody PaymentRequest request) {

        return paymentService.create(request);
    }
}

Frontend sends payment JSON data, and Spring converts it into PaymentRequest object automatically.


Advantages of @RequestBody

  • Automatic JSON conversion
  • Reduces boilerplate code
  • Easy REST API development
  • Supports validation
  • Improves readability
  • Simplifies frontend-backend communication

Production-Level Importance

@RequestBody is heavily used in:

  • REST APIs
  • Microservices
  • Banking applications
  • E-commerce platforms
  • Cloud-native systems
  • Mobile backend APIs

It simplifies JSON request handling in enterprise applications.


Interview Answer

@RequestBody is used in Spring Boot to convert HTTP request body data, usually JSON, into Java objects automatically.

It is commonly used in POST and PUT REST APIs.

Spring internally uses Jackson library for JSON conversion.


Short Interview Answer

@RequestBody converts incoming JSON request data into Java objects automatically.


Frequently Asked Questions

What is the purpose of @RequestBody?

To convert JSON request body into Java object.

Which library converts JSON to Java object?

Jackson library.

Can @RequestBody be used with GET request?

Technically possible but not recommended.

Which HTTP methods commonly use @RequestBody?

POST, PUT, and PATCH.

Can @RequestBody be combined with validation?

Yes, using @Valid.


Conclusion

@RequestBody is one of the most important annotations in Spring Boot REST API development.

It simplifies JSON request handling, improves frontend-backend communication, and supports clean API development.

It is widely used in enterprise applications, microservices, and cloud-native systems.

Why this Spring Boot question is important?

This interview question helps candidates understand real-time backend development concepts, practical problem solving, coding fundamentals, system design basics and production-ready application behavior.

Practice this question carefully for Java backend roles, Spring Boot developer interviews, microservices interviews, company interviews and full-stack developer preparation.

About the Author

Naresh Kumar is a Senior Java Backend Engineer with experience building enterprise applications using Java, Spring Boot, Microservices, Docker, Kubernetes and Cloud technologies.