Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@RequestParam vs @PathVariable

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

like image 562
user1857181 Avatar asked Dec 05 '12 03:12

user1857181


People also ask

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

Difference between @PathVariable and @RequestParam in Spring 1) The @RequestParam is used to extract query parameters while @PathVariable is used to extract data right from the URI.

What's the main difference between @PathVariable and RequestParam?

The key difference between @RequestParam and @PathVariable is that @RequestParam used for accessing the values of the query parameters where as @PathVariable used for accessing the values from the URI template.

Which is better request Param or path variable?

2) @RequestParam is more useful on a traditional web application where data is mostly passed in the query parameters while @PathVariable is more suitable for RESTful web services where URL contains values.

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.


2 Answers

  • @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 a parameter from the URI as well — see Spring Reference Chapter 16.3.3.3 Binding request parameters to method parameters with @RequestParam

If the 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:

@RequestMapping(value="/user/{userId}/invoices", method = RequestMethod.GET) public List<Invoice> listUsersInvoices(             @PathVariable("userId") int user,             @RequestParam(value = "date", required = false) Date dateOrNull) {   ... } 

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 user null or details about a user with ID "invoices"?

like image 165
Ralph Avatar answered Oct 02 '22 21:10

Ralph


@RequestParam annotation used for accessing the query parameter values from the request. Look at the following request URL:

http://localhost:8080/springmvc/hello/101?param1=10&param2=20 

In the above URL request, the values for param1 and param2 can be accessed as below:

public String getDetails(     @RequestParam(value="param1", required=true) String param1,         @RequestParam(value="param2", required=false) String param2){ ... } 

The following are the list of parameters supported by the @RequestParam annotation:

  • defaultValue – This is the default value as a fallback mechanism if request is not having the value or it is empty.
  • name – Name of the parameter to bind
  • required – Whether the parameter is mandatory or not. If it is true, failing to send that parameter will fail.
  • value – This is an alias for the name attribute

@PathVariable

@PathVariable identifies the pattern that is used in the URI for the incoming request. Let’s look at the below request URL:

http://localhost:8080/springmvc/hello/101?param1=10&param2=20

The above URL request can be written in your Spring MVC as below:

@RequestMapping("/hello/{id}")    public String getDetails(@PathVariable(value="id") String id,     @RequestParam(value="param1", required=true) String param1,     @RequestParam(value="param2", required=false) String param2){ ....... } 

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

http://localhost:8080/spring_3_2/matrixvars/stocks;BT.A=276.70,+10.40,+3.91;AZN=236.00,+103.00,+3.29;SBRY=375.50,+7.60,+2.07

And the Controller method for it

 @RequestMapping(value = "/{stocks}", method = RequestMethod.GET)   public String showPortfolioValues(@MatrixVariable Map<String, List<String>> matrixVars, Model model) {      logger.info("Storing {} Values which are: {}", new Object[] { matrixVars.size(), matrixVars });      List<List<String>> outlist = map2List(matrixVars);     model.addAttribute("stocks", outlist);      return "stocks";   } 

But you must enable:

<mvc:annotation-driven enableMatrixVariables="true" > 
like image 37
Xelian Avatar answered Oct 02 '22 22:10

Xelian