What is main difference between @RequestParam
and @QueryParam
in Spring MVC controller?
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.
@RequestParam annotation enables spring to extract input data that may be passed as a query, form data, or any arbitrary custom data.
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.
The @QueryParam annotation allows you to map a URI query string parameter or url form encoded parameter to your method invocation.
@QueryParam
is a JAX-RS framework annotation and @RequestParam
is from Spring.
Apart from these mentioned differences in framework, one major difference is @RequestParam
will always expect a value to bind. Hence, if value is not passed, it will give error. This is not the case in @QueryParam
To explicitly give the option, use required = false while using @RequestParam
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