In an example such as the following, what's the difference between a @PathVariable
and a @RequestParam
?
@RequestMapping(value = "/portfolio/{portfolioIdPath}", method = RequestMethod.GET)
public final String portfolio(HttpServletRequest request, ModelMap model,
@PathVariable long portfolioIdPath, @RequestParam long portfolioIdRequest)
The @PathVariable annotation is used for data passed in the URI (e.g. RESTful web services) while @RequestParam is used to extract the data found in query parameters.
What is main difference between @RequestParam and @QueryParam in Spring MVC controller? They're functionally the same: they let you bind the value of a named HTTP param to the annotated variable. That being said, the question is very broad, so you'll have to specify more detail if you want a more useful answer.
So basically, while @RequestBody maps entire user request (even for POST) to a String variable, @RequestParam does so with one (or more - but it is more complicated) request param to your method argument.
@RequestParam is a Spring annotation used to bind a web request parameter to a method parameter. It has the following optional elements: defaultValue - used as a fallback when the request parameter is not provided or has an empty value. name - name of the request parameter to bind to.
@RequestParam binds a request parameter to a parameter in your method. In your example, the value of the parameter named "portfolioIdRequest" in the GET request will be passed as the "portfolioIdRequest" argument to your method. A more concrete example - if the request URL is
http://hostname/portfolio/123?portfolioIdRequest=456
then the value of the parameter "portfolioIdRequest" will be "456".
More info here: http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/mvc.html#mvc-ann-requestparam
@PathVariable similarly binds the value of the URI template variable "portfolioIdPath" to the method parameter "portfolioIdPath". For example, if your URI is
/portfolio/123
then the value of "portfolioIdPath" method parameter will be "123".
More info here: http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/mvc.html#mvc-ann-requestmapping-uri-templates
@RequestParam identifies the HTTP GET or POST parameter which is sent by the client(user), And @RequestMapping extracts a segment of URL which varies from request to request:
http://host/?var=1
In the above URL "var" is a requestparam.
http://host/registration/{which}
and above URL's {which} is a request mapping. You could call your service like :
http://host/registration/user
OR like
http://host/registration/firm
In your application you can access the value of {which} (In first case which="user" and in second which="firm".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With