简体   繁体   中英

In a Spring 3.0 GET request, what's the difference between a @PathVariable and a @RequestParam

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 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".

It depends the way you want to handle your request

@RequestParam example
(request)http://something.com/owner?ownerId=1234

@PathVariable example
(request) http://something.com/owner/1234
(in tour code) /owner/{ownerId}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM