0
0
SpringbootComparisonBeginner · 4 min read

@PathVariable vs @RequestParam in Spring Boot: Key Differences & Usage

@PathVariable extracts values from URI path segments, while @RequestParam extracts values from query parameters or form data in Spring Boot. Use @PathVariable for mandatory URL parts and @RequestParam for optional or named parameters.
⚖️

Quick Comparison

This table summarizes the main differences between @PathVariable and @RequestParam in Spring Boot.

Aspect@PathVariable@RequestParam
Source of dataURI path segmentQuery parameter or form data
Usage in URLPart of URL path (e.g., /users/{id})After ? in URL (e.g., ?id=123)
Mandatory by defaultYes, must be presentNo, can be optional with defaultValue or required=false
Typical use caseIdentifying resource by ID or nameFiltering, sorting, or optional inputs
Annotation placementOn method parameter matching path variableOn method parameter matching query/form parameter
Supports multiple parametersYes, multiple path variablesYes, multiple request parameters
⚖️

Key Differences

@PathVariable binds a method parameter to a URI template variable. It is used when the value is part of the URL path itself, making it a required and integral part of the resource identification. For example, in /users/{id}, the id is a path variable that uniquely identifies a user.

On the other hand, @RequestParam binds a method parameter to a query parameter or form data. It is typically used for optional parameters or filters that modify the request but are not part of the resource path. For example, /users?sort=asc uses a request parameter to specify sorting order.

Another difference is that @PathVariable parameters are mandatory by default and will cause an error if missing, while @RequestParam can be optional by setting required=false or providing a defaultValue. This makes @RequestParam more flexible for optional inputs.

⚖️

Code Comparison

java
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserController {

    @GetMapping("/users/{id}")
    public String getUserById(@PathVariable String id) {
        return "User ID from path: " + id;
    }
}
Output
User ID from path: 42 (when accessing /users/42)
↔️

@RequestParam Equivalent

java
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserController {

    @GetMapping("/users")
    public String getUserById(@RequestParam String id) {
        return "User ID from query param: " + id;
    }
}
Output
User ID from query param: 42 (when accessing /users?id=42)
🎯

When to Use Which

Choose @PathVariable when the value is a required part of the URL path that identifies a specific resource, such as an ID or name in RESTful APIs. It makes URLs clean and semantic, showing resource hierarchy clearly.

Choose @RequestParam when the value is optional or used for filtering, sorting, or additional parameters that modify the request but do not identify the resource itself. It is flexible for optional inputs and query-based parameters.

Key Takeaways

@PathVariable extracts data from the URL path and is mandatory by default.
@RequestParam extracts data from query parameters and can be optional.
Use @PathVariable for resource identification in RESTful URLs.
Use @RequestParam for optional filters or parameters.
Both annotations can handle multiple parameters but serve different URL parts.