Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)
like image 990
August Avatar asked Nov 01 '10 23:11

August


People also ask

What is the difference between @RequestParam and @PathVariable annotation?

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 the difference between @RequestParam and query param?

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.

What is the difference between @RequestParam and @RequestBody?

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.

What is @RequestParam in Spring?

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


2 Answers

@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

like image 143
benw Avatar answered Oct 14 '22 17:10

benw


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

like image 25
Erhan Bagdemir Avatar answered Oct 14 '22 18:10

Erhan Bagdemir