Explain @RequestBody annotation
The `@RequestBody` annotation in Spring is used to bind the HTTP request body to a method parameter in a controller. It is typically used to handle JSON or XML data sent in the body of a POST or PUT request. When you use `@RequestBody`, Spring automatically deserializes the incoming data into the specified Java object, making it easier to work with complex data in your controller methods.
Table of Contents
Example of RequestBody annotation
Step 1: Create a Spring Boot Application
Ensure you have the necessary dependencies in your `pom.xml` (for Maven) or `build.gradle` (for Gradle).
Maven Dependency:
```xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
```
Gradle Dependency:
```groovy
implementation 'org.springframework.boot:spring-boot-starter-web'
```
Step 2: Create the Main Application Class
Create the main class for the Spring Boot application.
```java
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
```
Explanation:
@SpringBootApplication
: Indicates a configuration class that declares one or more@Bean
methods and also triggers auto-configuration and component scanning.
Step 3: Create a Data Transfer Object (DTO)
Create a simple DTO class that will represent the data structure.
```java
package com.example.demo.dto;
public class UserDto {
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;
}
}
```
Explanation:
- DTO (Data Transfer Object): A simple Java class with private fields and public getters and setters, used to transfer data.
Step 4: Create a Controller
Create a controller class that uses the @RequestBody
annotation to handle incoming data.
```java
package com.example.demo.controller;
import com.example.demo.dto.UserDto;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/api")
public class UserController {
@PostMapping("/users")
public String createUser(@RequestBody UserDto userDto) {
return "User created: " + userDto.getName() + ", Age: " + userDto.getAge();
}
}
```
Explanation:
@RestController
: Indicates that this class is a REST controller, and the return values of methods are directly written to the HTTP response body.@RequestMapping("/api")
: Specifies that all methods in this class handle requests starting with/api
.@PostMapping("/users")
: Maps POST requests to/api/users
to thecreateUser
method.@RequestBody
: Binds the incoming request body to theUserDto
object. Spring automatically deserializes the JSON data in the request body into aUserDto
instance.
Step 5: Running the Application
Run the application from the main class (DemoApplication
). You can test the endpoint using a tool like Postman by sending a POST request to http://localhost:8080/api/users
with a JSON body:
```json
{
"name": "John Doe",
"age": 30
}
```
The response should be: "User created: John Doe, Age: 30"
.
Conclusion of RequestBody annotation
@RequestBody
: Used to bind the HTTP request body to a method parameter in a controller, facilitating the handling of complex data structures.- Example: Demonstrated how to use
@RequestBody
to handle JSON data sent in a POST request and map it to a Java object.