What is the difference between QueryParam and PathParam in terms of their usage?
I understand that data can be passed to service using both of them. PathParam is the data preceeding ? in the URL and QueryParam are name value data after ?. But I wonder how exactly these are used.
URI parameter (Path Param) is basically used to identify a specific resource or resources whereas Query Parameter is used to sort/filter those resources. Let's consider an example where you want identify the employee on the basis of employeeID, and in that case, you will be using the URI param.
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. @PathParam is a JAX-RS annotation that is equivalent to @PathVariable in Spring.
Basically, @QueryParam denotes that the value of the Query Parameter with the corresponding name will be parsed, and if parsed correctly it will be available on the method argument denoted with @QueryParam . There are baically two ways to pass parameters in a GET request in REST services.
@PathParam is a parameter annotation which allows you to map variable URI path fragments into your method call.
@QueryParam
is used to access key/value pairs in the query string of the URL (the part after the ?). For example in the url http://example.com?q=searchterm
, you can use @QueryParam("q")
to get the value of q
.
@PathParam
is used to match a part of the URL as a parameter. For example in an url of the form http://example.com/books/{bookid}
, you can use @PathParam("bookid")
to get the id of a book.
See this page for an example as used in JAX-RS.
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