@RequestParam
Reference: Spring Docs - RequestParam
What is @RequestParam?
An annotation that retrieves parameters passed from external sources to an API
Used to bind HTTP request parameters to method parameters
Mainly used when receiving parameter values from client requests as Controller method parameters for processing
ex)
@RequestParam("name") String nameStores the parameter passed externally with the name "name" into the method parameter name (String name)
Using @RequestParam
Spring MVC
Maps to query parameters, form data, and parts of multipart requests
This occurs because the Servlet API combines query parameters and form data into a single map called
parameters, which includes automatic parsing of the request body
Spring WebFlux
Maps only to query parameters
To handle all three types -- query parameters, form data, and multipart -- you can use data binding to a command object annotated with
ModelAttribute
Things to know
If the method parameter type is Map and a request parameter name is specified, the request parameter value is converted to a Map when an appropriate conversion strategy exists
If the method parameter is
Map<String, String>orMultiValueMap<String, String>and no parameter name is specified, the map parameter is populated with all request parameter names and values
Attributes of @RequestParam
nameSpecifies the name of the request parameter
requiredSpecifies whether the request parameter is required
Default value is
trueIf
required=trueand the parameter is not present in the request, an exception is thrown
defaultValueSpecifies the default value to use when the request parameter is not provided
valueAlias for name()
Last updated