7.2.2 Paging and sorting
You may have noticed that the links in the home resource all offer optional page, size, and sort parameters. By default, requests to a collection resource such as /dataapi/tacos will return up to 20 items per page from the first page. But you can adjust the page size and the page displayed by specifying the page and size parameters in your request.
For example, to request the first page of tacos where the page size is 5, you can issue the following GET request (using curl):
$ curl "localhost:8080/data-api/tacos?size=5"Assuming there are more than five tacos to be seen, you can request the second page of tacos by adding the page parameter as follows:
$ curl "localhost:8080/data-api/tacos?size=5&page=1"Notice that the page parameter is zero-based, which means that asking for page 1 is actually asking for the second page. (You’ll also note that many command-line shells trip up over the ampersand in the request, which is why I quoted the whole URL in the preceding curl command.)
The sort parameter lets you sort the resulting list by any property of the entity. For example, you need a way to fetch the 12 most recently created tacos for the UI to display. You can do that by specifying the following mix of paging and sorting parameters:
$ curl "localhost:8080/data-api/tacos?sort=createdAt,desc&page=0&size=12"Here the sort parameter specifies that you should sort by the createdDate property and that it should be sorted in descending order (so that the newest tacos are first). The page and size parameters specify that you should see the first page of 12 tacos.
This is precisely what the UI needs to show the most recently created tacos. It’s approximately the same as the /api/tacos?recent endpoint you defined in TacoController earlier in this chapter.
Now let’s switch gears and see how to write client code to consume the API endpoints we’ve created.
