Spring Boot - @PathVariable and @RequestParam Annotations

Last Updated : 10 Mar, 2026

In Spring Boot, handling data from client requests is a common task when building REST APIs. Two widely used annotations for retrieving values from a request URL are @PathVariable and @RequestParam, which help map client inputs to controller method parameters.

Both annotations are part of the Spring Boot web layer and are used in Spring MVC controllers to extract values sent by users in HTTP requests.

@PathVariable

@PathVariable is used to extract values from the URI path of a request. It is commonly used in REST APIs where resources are identified directly within the URL.

  • It binds a method parameter to a URI template variable.
  • It is mostly used for identifying specific resources in RESTful endpoints.

Example:

Java
@GetMapping("/users/{id}")
public String getUserById(@PathVariable int id) {
    return "User ID: " + id;
}

In this example a GET API endpoint that retrieves the id value from the URL path using @PathVariable and returns it as part of the response.

@RequestParam

@RequestParam is used to extract values from query parameters in the URL. It is commonly used when sending optional data, filters, or search parameters in requests.

  • It binds a method parameter to a query parameter in the URL.
  • It can be required or optional, and also supports default values.

Example:

Java
@RestController
@RequestMapping("/users")
public class UserController {

    @GetMapping("/search")
    public String searchUser(@RequestParam String name) {
        return "Searching user with name: " + name;
    }
}

Explanation: @RequestParam extracts the name value from the query parameter in the URL. If the request is /users/search?name=John, the method receives "John" as the value of the name parameter.

Example: How to define a GET endpoint that reads id from the URL using @PathVariable.

Java
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

@RestController
@RequestMapping("/users")
class UserController {

    @GetMapping("/{userId}")
    public ResponseEntity<String> getUserDetails(@PathVariable Long userId) {
        // Implementation to fetch user details based on the provided userId
        String userDetails = "User details for user ID: " + userId;
        return ResponseEntity.ok(userDetails);
    }

    @GetMapping("/search")
    public ResponseEntity<List<String>> searchUsers(@RequestParam("name") String name) {
        // Implementation to search users based on the provided name
        List<String> users = Arrays.asList("John", "Jane", "Robert");
        return ResponseEntity.ok(users);
    }
}

Explanation

  • The Application class is the entry point for the Spring Boot application. It is annotated with @SpringBootApplication.
  • The UserController class is annotated with @RestController, indicating that it is a REST controller.
  • The base URL path for all user-related endpoints are defined using @RequestMapping("/users").
  • The getUserDetails method is mapped to the /{userId} URL pattern using @GetMapping("/{userId}"). The userId path variable is extracted using @PathVariable and passed as a method parameter.
  • The searchUsers method is mapped to the /search URL pattern using @GetMapping("/search"). The name query parameter is extracted using @RequestParam("name") and passed as a method parameter.
  • You can run this Spring Boot application, and it will provide two endpoints: /users/{userId} and /users/search. You can send requests to these endpoints with appropriate path variables and query parameters to see the respective data extraction in action.

Note: Make sure you have the necessary dependencies and configurations set up in your Spring Boot project to run the application successfully.

@PathVariable Vs @RequestParam Annotations

Feature@PathVariable@RequestParam
DefinitionUsed to extract values from the URL path.Used to extract values from query parameters in the URL.
URL PositionValue appears inside the URL path.Value appears after ? as key-value pairs.
PurposeMainly used to identify a specific resource like an ID.Mainly used to pass filters, search values, or optional data.
URL Example/users/101/users/search?name=John
Annotation Example@GetMapping("/users/{id}")@GetMapping("/users/search?name=John")
Comment

Explore