when to use query parameters vs path parameters code example
Example 1: query and path parameters
2 TYPES OF PARAMETERS IN REST SERVICES:
1) QUERY PARAMETERS:
-> is NOT part of url and passed in key=value format
those parameters must be defined by API developer
http://34.223.219.142:1212/ords/hr/employees?limit=100
Query parameters are the most common type of parameters.
They appear at the end of the request URL after a question mark (?),
with different name=value pairs separated by ampersands (&).
Query parameters can be required and optional.
GET /pets/findByStatus?status=available
GET /notes?offset=100&limit=50
2) PATH PARAMETERS
Path parameters are variable parts of a URI path. They are typically
used to point to a specific resource within a collection, such as a user
identified by ID. A URL can have several path parameters, each
denoted with curly braces { }.
GET /users/{id}
GET /cars/{carId}/drivers/{driverId}
GET /report.{format}
Example 2: query vs path parameters
2 TYPES OF PARAMETERS IN REST SERVICES:
1) QUERY PARAMETERS:
-> is NOT part of url and passed in key=value format
those parameters must be defined by API developer
http://34.223.219.142:1212/ords/hr/employees?limit=100
Query parameters are the most common type of parameters.
They appear at the end of the request URL after a question mark (?),
with different name=value pairs separated by ampersands (&).
Query parameters can be required and optional.
GET /pets/findByStatus?status=available
GET /notes?offset=100&limit=50
2) PATH PARAMETERS
Path parameters are variable parts of a URI path. They are typically
used to point to a specific resource within a collection, such as a user
identified by ID. A URL can have several path parameters, each
denoted with curly braces { }.
GET /users/{id}
GET /cars/{carId}/drivers/{driverId}
GET /report.{format}
When to use ??
If there is a scenario to retrieve a record based on id, for example
you need to get the details of the employee whose id is 15, then you
can have resource with @PathParam.
GET /employee/{id}
If there is a scenario where you need to get the details of all employees
but only 10 at a time, you may use query param
GET /employee?start=1&size=10
(This says that starting employee id 1 get ten records.)
To summarize, use @PathParam for retrieval based on id. User
@QueryParam for filter