What is the purpose of the @RequestParam annotation?
Table of Contents
- Introduction
- 1. Binding Query Parameters to Method Parameters
- Explanation:
- 2. Handling Optional Query Parameters
- Explanation:
- 3. Handling Multiple Query Parameters
- Explanation:
- 4. Binding Multiple Values to a Single Parameter
- Explanation:
- 5. Binding Query Parameters to Custom Objects
- Explanation:
- 6. Customizing Parameter Binding
- Explanation:
- 7. Conclusion
Introduction
In Spring MVC and Spring Boot, the @RequestParam
annotation is used to extract query parameters from an HTTP request and bind them to the method parameters of a controller. It is one of the most commonly used annotations for handling HTTP GET requests, especially when dealing with parameters passed in the URL.
When building web applications or REST APIs, you often need to capture user input or request details. These inputs might be sent via URL parameters (query parameters) or form data. The @RequestParam
annotation makes it easy to access and work with these parameters in your controller methods.
1. Binding Query Parameters to Method Parameters
The primary purpose of @RequestParam
is to bind query parameters from the HTTP request to method parameters in a Spring controller. Query parameters are typically included in the URL after the ?
symbol, and they follow the pattern key=value
.
Example: Simple Use of @RequestParam
URL Example:
http://localhost:8080/search?query=laptop
Explanation:
**@RequestParam String query**
: Thequery
parameter from the URL (e.g.,?query=laptop
) is automatically mapped to thequery
parameter of thesearch
method.- The response will be
"Searching for: laptop"
.
2. Handling Optional Query Parameters
In many cases, a query parameter may be optional. If a parameter is not provided in the URL, Spring can assign a default value or leave the parameter as null
(if the required
attribute is set to false
).
Example: Optional Query Parameters
URL Examples:
http://localhost:8080/search?query=laptop
→"Searching for: laptop"
http://localhost:8080/search
→"Searching for: all"
Explanation:
**required = false**
: Makes thequery
parameter optional.**defaultValue = "all"**
: Provides a default value forquery
if it is not included in the URL. In this case, the default value is"all"
.
3. Handling Multiple Query Parameters
When handling multiple query parameters, you can add multiple @RequestParam
annotations to the method, each corresponding to a different query parameter.
Example: Multiple Query Parameters
URL Example:
http://localhost:8080/products?category=electronics&limit=5
Explanation:
**category**
and**limit**
: These two parameters are mapped from the query string to the corresponding method parameters. The URL includes bothcategory=electronics
andlimit=5
, which are mapped to thecategory
andlimit
method parameters, respectively.
4. Binding Multiple Values to a Single Parameter
@RequestParam
can also handle cases where the same parameter appears multiple times in the URL (e.g., when you have an array of values). Spring automatically maps multiple occurrences of the same parameter to a List
or Set
.
Example: Multiple Values for a Query Parameter
URL Example:
http://localhost:8080/categories?categories=electronics&categories=clothing&categories=home
Explanation:
**List<String> categories**
: Spring will collect all the values passed with thecategories
query parameter into aList
. The resulting list will contain["electronics", "clothing", "home"]
, and the response will be"Categories: electronics, clothing, home"
.
5. Binding Query Parameters to Custom Objects
For more complex query parameter handling, you can bind multiple query parameters into a custom Java object using the @ModelAttribute
annotation. Although @RequestParam
is not directly used for this, it's worth mentioning as a powerful technique for handling multiple parameters.
Example: Binding Query Parameters to a Custom Object
URL Example:
http://localhost:8080/search?category=electronics&minPrice=100&maxPrice=500
Explanation:
**@ModelAttribute**
: This binds multiple query parameters (category
,minPrice
,maxPrice
) to the fields of theProductSearchCriteria
object. Spring automatically maps the query parameters to the object fields.
6. Customizing Parameter Binding
Spring allows customization in parameter binding, such as using a custom converter or validating parameters before they are processed.
Example: Custom Conversion for Query Parameters
URL Example:
http://localhost:8080/products?minPrice=100&maxPrice=500
Explanation:
- Spring automatically converts the query string parameters (
minPrice
andmaxPrice
) toDouble
type, which is passed to the controller method.
7. Conclusion
The @RequestParam
annotation is a powerful and essential tool for handling query parameters in Spring MVC. It simplifies the process of extracting data from the URL and binding it to Java method parameters. Key features of @RequestParam
include:
- Binding query parameters to method parameters.
- Making parameters optional or providing default values.
- Handling multiple query parameters.
- Supporting collections (e.g., lists) for parameters with multiple values.
- Binding complex query data to custom objects.
By using @RequestParam
, you can efficiently process client input, making it easier to create dynamic, user-driven web applications and APIs.es. C