Open In App

Difference between @RequestBody and @RequestParam

In Spring Boot, the @RequestBody is used for binding the HTTP request body to a parameter in a controller method and it is typically used when we expect the client to send data in the request body when submitting a form or sending JSON data. The @RequestParam is one of the powerful full annotations in Spring Boot. This annotation can enable the Spring Boot framework to extract input data passed through an HTTP URL or passed as a query.

In this article, we will learn about @RequestBody vs @RequestParam annotation.

@RequestBody

The @RequestBody is used to bind the HTTP request body to a parameter in the controller method and is typically used when we expect the client to send data in the request body, such as a form to send or JSON data to send.

The Request body look like a below syntax. And The request body is plays an important role in data transform from Client to Server.

{
"username":"John",
"age":"21"
}

Example:

Below, we provide an API URL for testing @RequestBody in Spring Boot. This API has a POST type.

http://localhost:8080/user

User.class:

package com.app;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@AllArgsConstructor
@NoArgsConstructor
@Data
public class User {
    private String username;
    private int age;
}

MyHandler.class:

package com.app;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MyController {

    // Endpoint to handle POST requests for creating a user
    @PostMapping("/user")
    public ResponseEntity<String> createUser(@RequestBody User user) {
        // Logic to create a user and return response
        return ResponseEntity.ok("User created: " + user.toString());
    }
}

Output:

Once the logic is developed, we run this project as a Spring Boot App. Now, we open Postman to test the API endpoint. Upon testing, we received the following output.


Output for @RequestBody

@RequestParam

Example:

Now, we created one more API in the MyController class called "users".

http://localhost:8080/users?id=1
package com.app;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MyController {
    
    // Define a method to handle GET requests to the "/users" endpoint
    @GetMapping("/users")
    // Method to retrieve user details based on the provided user ID parameter
    public ResponseEntity<String> getUser(@RequestParam("id") Long userId) {
        // Return a ResponseEntity with the user details for the specified user ID
        return ResponseEntity.ok("User details for ID " + userId);
    }
}

Output:

Once the logic is developed, we run this project as a Spring Boot App. Now, we open Postman to test the API endpoint. Upon testing, we received the following output.


Output for @RequesrtParam


Difference between @RequestBody and @RequestParam

Below, we have provided the difference between @RequestBody and @RequestParam in the Spring Boot framework.

Feature

@RequestBody

@RequestParam

Usage

It is used to bind the HTTP request body to the parameter in the controller method.

It is used to extract parameters from the query string or form data of an HTTP request.

HTTP Method

Used with POST, PUT, and PATCH requests where data is sent in the request body.

Used with GET, POST, and other types of requests where parameters are sent in the URL.

Data Type

Suitable for complex data types such as objects or lists.

Used for simple data types such as strings or numbers.

Request Format

Expects data in JSON, XML, or other structured formats in the request body.

Expects parameters in the query string or form data format.

Example

@PostMapping("/users")

@GetMapping("/users")


Article Tags :