@Requestparam VS @Pathvariable

When to choose @RequestParam over @PathVariable and vice-versa?

@PathVariable identifies the pattern that is used in the URI for the incoming request.

Query args (@RequestParam) are used on querying/searching resources exclusively. They contain data that affects the query.

Here are some tips for creating good URLs.

When to go for @RequestParam and @PathVariable

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.

Difference between @PathVariable, @RequestParam, and @RequestBody

@PathVariable is for parts of the path (i.e. /person/{id})

@RequestParam is for the GET query parameters (i.e. /person?name="Bob").

@RequestBody is for the actual body of a request.

when should I use PathVariable and when should I use RequestParam

It depends on your design choices, i.e. whether you want to have information in your path or the query part. Using REST you would normally put resource identifiers into the path and additional parameters into the query, e.g. like this (made up):

/questions/67156664/comments?count=5

This would mean:

  • comments for question with id 67156664 (path variable)
  • return up to 5 comments (query param)

Note again, that it often depends on your requirements, i.e. what part of your url you want to put the parameters in.

Comparison: usage of @RequestMapping vs @PathVariable

"URI parameter (Path Param/variable denoted by @PathVariable) is basically used to identify a specific resource or resources whereas Query Parameter(or request param denoted by @RequestParam) is used to sort/filter those resources."
The first example makes use of path variable, here you are extracting value from the url itself, whereas in second example you are fetching a request/query parameter.
References:
https://dzone.com/articles/understanding-the-uri-param-and-query-param-with-r
https://blog.restcase.com/7-rules-for-rest-api-uri-design/#:~:text=URIs%20should%20follow%20a%20predictable,APIs%20are%20written%20for%20consumers.

What is the difference between @PathParam and @PathVariable

@PathVariable and @PathParam both are used for accessing parameters from URI Template

Differences:

  • As you mention @PathVariable is from spring and @PathParam is from JAX-RS.
  • @PathParam can use with REST only, where @PathVariable used in Spring so it works in MVC and REST.

@PathVariable vs @RequestParam to retrieve an entity

The path part of a URL identifies a resource, and a query string represents a query. While a query can certainly return exactly one item, a collection-resource URL (/users) should return an array of items (from zero to infinity), while a record-resource URL (/users/123) should return exactly one item (and this is the URL that should handle DELETE/PUT requests).

If the username is actually the canonical ID for the user, it should be a path variable; if there is a different canonical ID (such as a UUID), but you're searching by username, then use a query parameter but return a collection of zero or one results. (You can use params=username in the request mapping to only match the controller when that query parameter is present.)

Use @RequestParam or @PathVariable?

Rest convention is to use uri variables :

get http://stackoverflow.com/users/3630157


Related Topics



Leave a reply



Submit