@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 data | URI path segment | Query parameter or form data |
| Usage in URL | Part of URL path (e.g., /users/{id}) | After ? in URL (e.g., ?id=123) |
| Mandatory by default | Yes, must be present | No, can be optional with defaultValue or required=false |
| Typical use case | Identifying resource by ID or name | Filtering, sorting, or optional inputs |
| Annotation placement | On method parameter matching path variable | On method parameter matching query/form parameter |
| Supports multiple parameters | Yes, multiple path variables | Yes, 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
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; } }
@RequestParam Equivalent
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; } }
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.@PathVariable for resource identification in RESTful URLs.@RequestParam for optional filters or parameters.