In this article, we will see difference between PathParam and QueryParam and how to decide whether to use PathParam vs QueryParam in rest api design.
Diff between PathParam and QueryParam
Path params are part of the url where as query parameters are added after the ? mark symbol and separated from other query parameters by & symbol.
PathParam example
GET http://base-url/students/{roll-number}
QueryParam example
GET http://base-url/students?grade=10
When to use @PathParam vs @QueryParam
This is not a standard, you can use anyone for designing restful api.
However, the commonly used convention is :
- Any required or mandatory attributes should be added as path param
- Any optional attributes should be added as query param
- params used for filtering data are usually used as query param
Path param example
Finding employee with ID = 12345
GET http://base-url/employees/{12345}
Query param example
Finding active employees with title as developer
GET http://base-url/employees?title=developer&status=active
Both Path params and Query params
GET ://base-url/dept/1/employees/title=developer
© 2019, www.topjavatutorial.com. All rights reserved. On republishing this post, you must provide link to original post