@RequestParam vs @QueryParam vs @PathVariable vs @PathParam
Here's a comparison of the four annotations commonly used in Spring and JAX-RS (Java API for RESTful Web Services) for extracting parameters from a request:
@RequestParam
(Spring Framework):
Typically used for GET requests with URL parameters or form submissions.
2. @QueryParam
(JAX-RS):
@RequestParam
in Spring but used in JAX-RS (used in frameworks like Jersey or RESTEasy).URL: /employees?department=HR
3. @PathVariable
(Spring Framework):
Used to extract variables from the URI path, typically used with RESTful endpoints where the parameter is part of the URL path.
Commonly used with dynamic URIs like /employees/{id}
.
4. @PathParam
(JAX-RS):
Similar to @PathVariable
in Spring but used in JAX-RS.
Extracts parameters from the URI path.
Feature | @RequestParam (Spring) | @QueryParam (JAX-RS) | @PathVariable (Spring) | @PathParam (JAX-RS) |
---|---|---|---|---|
Framework | Spring MVC / Spring Boot | JAX-RS (Jersey, RESTEasy, etc.) | Spring MVC / Spring Boot | JAX-RS (Jersey, RESTEasy, etc.) |
Usage | Used to extract query parameters or form data from the URL or body | Extracts query parameters from the URL | Extracts values from the path of the URL | Extracts values from the path of the URL |
HTTP Methods | Typically used in GET, POST, PUT, DELETE (for query/form data) | Typically used in GET, POST, PUT, DELETE (for query data) | Typically used in GET, POST, PUT, DELETE (for dynamic path segments) | Typically used in GET, POST, PUT, DELETE (for dynamic path segments) |
Type of Parameter | Query parameters or form parameters | Query parameters | Path parameters (part of the URL) | Path parameters (part of the URL) |
Example URL | /employees?department=HR | /employees?department=HR | /employees/123 | /employees/123 |
Method Example | public List<Employee> getEmployees(@RequestParam String department) | public List<Employee> getEmployees(@QueryParam("department") String department) | public Employee getEmployeeById(@PathVariable Long id) | public Employee getEmployeeById(@PathParam("id") Long id) |
Annotation Placement | Can be used with method parameters in controller classes | Can be used with method parameters in JAX-RS resource classes | Can be used with method parameters in controller classes | Can be used with method parameters in JAX-RS resource classes |
Default Value Support | Supports default values using defaultValue attribute | Supports default values using defaultValue attribute | Does not support default values | Does not support default values |
Required/Optional | Default is required, but can be made optional using required = false | Default is required, but can be made optional using defaultValue | Always required if the path is dynamic | Always required if the path is dynamic |
Common Use Case | For handling form data or query parameters like search filters or pagination | For handling query parameters in RESTful web services | For handling resource identifiers in RESTful web services | For handling resource identifiers in RESTful web services |
@RequestParam
/ @QueryParam
: Extract query parameters, often for filters, search criteria, or form data (e.g., ?page=1&size=10
).
@PathVariable
/ @PathParam
: Extract path parameters, commonly used for dynamic resource identifiers (e.g., /employees/{id}
).
Your email address will not be published. Required fields are marked *