When to use @QueryParam vs @PathParam

Viewed 502474

I am not asking the question that is already asked here: What is the difference between @PathParam and @QueryParam

This is a "best practices" or convention question.

When would you use @PathParam vs @QueryParam.

What I can think of that the decision might be using the two to differentiate the information pattern. Let me illustrate below my LTPO - less than perfect observation.

PathParam use could be reserved for information category, which would fall nicely into a branch of an information tree. 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.

For example,

  • /Vehicle/Car?registration=123
  • /House/Colonial?region=newengland

/category?instance

@GET
@Path("/employee/{dept}")
Patient getEmployee(@PathParam("dept")Long dept, @QueryParam("id")Long id) ;

vs /category/instance

@GET
@Path("/employee/{dept}/{id}")
Patient getEmployee(@PathParam("dept")Long dept, @PathParam("id")Long id) ;

vs ?category+instance

@GET
@Path("/employee")
Patient getEmployee(@QueryParam("dept")Long dept, @QueryParam("id")Long id) ;

I don't think there is a standard convention of doing it. Is there? However, I would like to hear of how people use PathParam vs QueryParam to differentiate their information like I exemplified above. I would also love to hear the reason behind the practice.

18 Answers

Before talking about QueryParam & PathParam. Let's first understand the URL & its components. URL consists of endpoint + resource + queryParam/ PathParam.

For Example,

URL: https://app.orderservice.com/order?order=12345678

or

URL: https://app.orderservice.com/orders/12345678

where

endpoint: https://app.orderservice.com

resource: orders

queryParam: order=12345678

PathParam: 12345678

@QueryParam:

QueryParam is used when the requirement is to filter the request based on certain criteria/criterias. The criteria is specified with ? after the resource in URL. Multiple filter criterias can be specified in the queryParam by using & symbol.

For Example:

https://app.orderservice.com/orders?order=12345678 & customername=X

@PathParam:

PathParam is used when the requirement is to select the particular order based on guid/id. PathParam is the part of the resource in URL.

For Example:

https://app.orderservice.com/orders/12345678

PATH PARAMETER - Path Parameter is a variable in URL path that helps to point some specific resource.

Example - https://sitename.com/questions/115

Here, if 115 is a path parameter it can be changed with other valid number to fetch/point to some other resource on the same application.

QUERY PARAMETER - Query Parameters are variables in URL path that filter some particular resources from the list.

Example - https://sitename.com/questions/115?qp1=val1&qp2=val2&qp3=val3

Here qp1, qp2 and qp3 are Query Variables with their values as val1, val2 and val3. These can be used to apply as filters while fetching/saving our data. Query variables are always appended in URL after a question Mark(?).

For resource names and IDs, I use @PathParams. For optional variables, I use @QueryParams

As per my understanding:

  1. Use @PathParam - when it is a mandatory item such as an Id

    GET /balloon/{id}

  2. Use @QueryParam - when you have the exact resource but need to filter that on some optional traits such as color, size, etc.

    GET /balloon/123?color=red&size=large

I prefer following :

@PathParam

When it's required parameters such as ID, productNo

GET /user/details/{ID}
GET /products/{company}/{productNo}

@QueryParam

When you need to pass optional parameters such as filters, online state and They can be null

GET /user/list?country=USA&status=online
GET /products/list?sort=ASC 

When Used both

GET /products/{company}/list?sort=ASC 
Related