I know what @RequestParam
does and what @PathVariable
does on an individual note but my question is when building a URL, when should I go for @RequestParam
and when for @PathVariable
? What are the pros and and cons of using both these annotations?
I did go through this link When to go for @RequestParam and @PathVariable but did not get a satisfactory answer anywhere.
@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.
It makes more sense to use @Pathvariable
to give the user an idea of what the page means.twitter.com/{userid}
@RequestParams
can be used to get info as parameter, like twitter.com?search=%23yolo
@Pathvariable
will help the user navigate or directly move to certain views/pages.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With