Book Image

RESTful Java Web Services, Second Edition

Book Image

RESTful Java Web Services, Second Edition

Overview of this book

Table of Contents (17 chapters)
RESTful Java Web Services Second Edition
Credits
About the Author
Acknowledgments
About the Reviewers
www.PacktPub.com
Preface
Index

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> findDepartments(@QueryParam("offset") 
@DefaultValue("0") Integer offset, @QueryParam("limit") @DefaultValue("20") Integer limit) {
    //Complete method implementation is not shown for brevity
    return findDepartmentEntitiesInRange(offset...