11

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?

kryger
  • 12,906
  • 8
  • 44
  • 65
chandu
  • 223
  • 1
  • 6
  • 13
  • @RequestParam for `GET` requests are good in case for example some kind of filtering feature. When @PathVariable is good for navigation overall. So for example, you have link like: http://my-web-site.com/user/:username?filter=name. – Yuri Jul 10 '15 at 13:46
  • you can use both they are exclusive of each other. one extract the path param from url hence if something goes wrong the error status can be 404 I.e. not found ... while the other extracts the request parameter appended by ? in url and separated by & if something goes wrong you can give 400 I.e bad request status error. hence they severe diff context. – Amit Kumar Lal Oct 18 '18 at 02:25

2 Answers2

12

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.

Kumar Sambhav
  • 7,503
  • 15
  • 63
  • 86
4

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.

Premraj
  • 72,055
  • 26
  • 237
  • 180
  • I would agree with Premraj because path defines specific resource/resources and not the structure of the returned data. So path contain only specific properties of a resource and query parameters should define how the data structure will look like. – Peter S. Apr 12 '19 at 13:51