Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to go for @RequestParam and @PathVariable

Just curious to know in which scenario we should go for @RequestParam and @PathVariable. I know that:

  1. @RequestParam takes parameter value whereas @PathVariable takes placeholder value
  2. @RequestParam can be optional (required=false) while making request whereas @PathVariable value has to be provided.
  3. When we want to use @RequestParam we have to know the property syntax but for @PathVariable not required

Is there any other reason to go for specific one?

like image 508
chandu Avatar asked Jul 10 '15 10:07

chandu


People also ask

Can we use @RequestParam and @PathVariable together?

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. These annotations can be mixed together inside the same controller.

When should you use path variable and query parameter?

PathParam could be used to drill down to entity class hierarchy. Whereas, QueryParam could be reserved for specifying attributes to locate the instance of a class.

What is the difference between @RequestParam and @PathVariable in Spring?

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 is difference between @RequestParam and QueryParam?

@QueryParam is a JAX-RS framework annotation and @RequestParam is from Spring. QueryParam is from another framework and you are mentioning Spring. @Flao wrote that @RequestParam is from Spring and that should be used in Spring MVC.


2 Answers

  • org.springframework.web.bind.annotation.RequestParam is used to bind Query String.
  • org.springframework.web.bind.annotation.PathVariable is used to bind URL path.
  • org.springframework.web.bind.annotation.RequestBody is used to bind HTTP Body.
  • org.springframework.http.RequestEntity will give you some added flexibility in defining arbitrary HTTP Entity headers along with HTTP Body.

Best Practice:

  • If you want to identify a resource, you should use Path Variable.
  • But if you want to sort or filter items, then you should use query parameter.

Example:

 /users # Fetch a list of users
/users?occupation=programmer # Fetch a list of user with filter programmer
/users/123 # Fetch a user who has id 123

you can get side effects. You don’t have to define other URL and other query parameter to achieve basic CRUD functions.You change HTTP method depends on what you want to do.

/users [GET] # Fetch a list of users
/users [POST] # Create new user
/users/123 [PUT] # Update user
/users/123 [DELETE] # remove user

Putting optional parameters in the Template URL will end up getting really messy, So I would recommend to put optional parameter in Query String.

like image 52
Premraj Avatar answered Oct 10 '22 19:10

Premraj


Use @PathVariable if you want to adhere to 'statefull' URLs.

For Example:-

/customer/:id   Customer view/edit page
/customer/      Customer Add page
/customer/list  List Customer Page
/customer/:cid/order  All order of a Customer
/customer/:cid/order/:oid  Specific order of a partucular Customer.

Wisely using Path Variable will result in URL that gives you hint/clue about what the resulting view/page means.

This also lets you support refresh,back & forward operation with no extra effort.

@RequestParams can be used to exatract data which is not passed as path params. Your MVC handler can have combination of two as required.

like image 32
Kumar Sambhav Avatar answered Oct 10 '22 17:10

Kumar Sambhav