Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java RESTful services - What is the difference between QueryParam and PathParam in terms of their usage?

Tags:

java

rest

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.

like image 868
Anil Avatar asked May 02 '12 14:05

Anil


People also ask

What is PathParam and QueryParam in REST api?

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.

What is the difference between PathVariable and PathParam?

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.

What is QueryParam in rest?

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.

What is PathParam in rest?

@PathParam is a parameter annotation which allows you to map variable URI path fragments into your method call.


1 Answers

@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.

like image 77
marcok Avatar answered Oct 03 '22 16:10

marcok