In a Spring 3.0 GET request, what's the differ

2020-06-03 01:55发布

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)

3条回答
你好瞎i
2楼-- · 2020-06-03 02:09

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

查看更多
Rolldiameter
3楼-- · 2020-06-03 02:12

@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

查看更多
Ridiculous、
4楼-- · 2020-06-03 02:17

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}
查看更多
登录 后发表回答