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)
@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:
In the above URL "var" is a requestparam.
and above URL's {which} is a request mapping. You could call your service like :
OR like
In your application you can access the value of {which} (In first case which="user" and in second which="firm".
@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
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
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
It depends the way you want to handle your request