What is the difference between @RequestParam
and @PathVariable
while handling special characters?
+
was accepted by @RequestParam
as space.
In the case of @PathVariable
, +
was accepted as +
.
What is the difference between @RequestParam
and @PathVariable
while handling special characters?
+
was accepted by @RequestParam
as space.
In the case of @PathVariable
, +
was accepted as +
.
@PathVariable
is to obtain some placeholder from the URI (Spring call it an URI Template) — see Spring Reference Chapter 16.3.2.2 URI Template Patterns@RequestParam
is to obtain an parameter from the URI as well — see Spring Reference Chapter 16.3.3.3 Binding request parameters to method parameters with @RequestParamIf URL
http://localhost:8080/MyApp/user/1234/invoices?date=12-05-2013
gets the invoices for user 1234 on December 5th, 2013, the controller method would look like:Also, request parameters can be optional, and as of Spring 4.3.3 path variables can be optional as well. Beware though: this might change the URL path hierarchy and introduce request mapping conflicts. For example, would
/user/invoices
provide the invoices for usernull
or details about a user with ID "invoices"?@RequestParam is use for query parameter(static values) like: http://localhost:8080/calculation/pow?base=2&ext=4
@PathVariable is use for dynamic values like : http://localhost:8080/calculation/sqrt/8
it may be that the application/x-www-form-urlencoded midia type convert space to +, and the reciever will decode the data by converting the + to space.check the url for more info.http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4.1
@RequestParam annotation used for accessing the query parameter values from the request. Look at the following request URL:
In the above URL request, the values for param1 and param2 can be accessed as below:
The following are the list of parameters supported by the @RequestParam annotation:
@PathVariable
@PathVariable identifies the pattern that is used in the URI for the incoming request. Let’s look at the below request URL:
The above URL request can be written in your Spring MVC as below:
The @PathVariable annotation has only one attribute value for binding the request URI template. It is allowed to use the multiple @PathVariable annotation in the single method. But, ensure that no more than one method has the same pattern.
Also there is one more interesting annotation: @MatrixVariable
And the Controller method for it
But you must enable: