Paging resource collection

It is not considered a good practice to return all resources that you may have in the database (or in any other data source) to a client in response to a GET API call. A very common approach for limiting the resource collection returned by an API is to allow the client to specify the offset and the page size for the collection. For example, the API that allows the client to specify the offset and the limit for the resource collection as the query parameters is /departments?offset=1&limit=20.

The following code snippet demonstrates how you can build a JAX-RS resource method that takes the offset and the page size (limit) sent by the client via query parameters:

@GET @Produces("application/json") public List<Department> ...

Get RESTful Java Web Services - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.