Difference RequestBody and ResponseBody

Difference RequestBody and ResponseBody

`@RequestBody`:

  • The `@RequestBody` annotation is used to bind the HTTP request body to a method parameter in a controller. It is used to deserialize the request body into a Java object.
  • Usage: Typically used when you need to handle data submitted in the body of an HTTP POST, PUT, or PATCH request. It converts the JSON or XML payload in the request body into a Java object.
  • Scope: Used for input data handling in the controller method.

`@ResponseBody`:

  • The `@ResponseBody` annotation is used to bind a method’s return value to the HTTP response body. It converts the Java object returned by the controller method into JSON, XML, or another format as RequestBody and ResponseBody specified by the `HttpMessageConverter`.
  • Usage: Typically used when you want to return data directly from a controller method, and it should be sent in the response body of the HTTP request.
  • Scope: Used for output data handling in the controller method.

RequestBody and ResponseBody

1. Using `@RequestBody`
Example: Handling Incoming Data

UserController.java
```java
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserController {

    @PostMapping("/user")
    public String createUser(@RequestBody UserDTO userDTO) {
        // Simulate processing the received data
        return "User created with username: " + userDTO.getUsername();
    }
}
```

UserDTO.java
```java
public class UserDTO {
    private String username;
    private String email;

    // Constructors, getters, and setters
    public UserDTO() {}
    
    public UserDTO(String username, String email) {
        this.username = username;
        this.email = email;
    }

    public String getUsername() { return username; }
    public void setUsername(String username) { this.username = username; }
    public String getEmail() { return email; }
    public void setEmail(String email) { this.email = email; }
}
```

In this example:

  • @RequestBody is used to bind the JSON payload in the request body to the UserDTO object.

Using @ResponseBody
2. Using `@ResponseBody`
Example: Returning Data

UserController.java
```java
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
public class UserController {

    @GetMapping("/info")
    @ResponseBody
    public UserDTO getUserInfo() {
        // Simulate returning user data
        return new UserDTO("john_doe", "john.doe@example.com");
    }
}
```

Example of RequestBody and ResponseBody

  • @ResponseBody is used to indicate that the UserDTO object returned from the getUserInfo method should be written directly to the HTTP response body as JSON. Summary of Differences

  • @RequestBody:
    • Purpose: Binds HTTP request body to a method parameter.
    • Usage: For deserializing input data from the request body into a Java object.
    • Example: Handling data submitted in a POST request.

  • @ResponseBody:
    • Purpose: Binds a method’s return value to the HTTP response body.
    • Usage: For serializing a Java object into JSON or XML to be sent in the response body.
    • Example: Returning data directly as a JSON response.

Homepage

Readmore