Learn how Spring Data REST maps special URL parameters to Spring Data JPA PagingAndSortingRepository interface methods.
- Another cool built-in feature of both Spring Data JPA and Spring Data Rest is the the ease of adding paging and sorting to an API. So, let's add Paging and Sorting to the Tours API by extending from Paging and Sorting Crud Repository. So, we're going to our Tour Repository and we're changing it to Paging and Sorting Repository, and we can also add Paging and Sorting to query methods by updating the return type to Page and adding the parameter 'Pageable.' So, on the URL, these are the parameters you would use for paging and sorting. There is the size parameter which is the size of the page that you want to retrieve.
The default for that is 20 if you don't provide it. The Page parameter is the page number you want to fetch, so that there are several pages, which one do you want where zero is the first, and the default, if you don't provide it, is zero. Sort is how you want the data sorted. By default, if you don't provide the Sort parameter, it will sort by the entity ID and if you want to give a direction of your sort, you can optionally put in asc for ascending or desc for descending.
So let's run our new Paging and Sorting Repository. So, now we are going to invoke the Tours API, but with the Paging and Sorting parameters. So, with size equals three, that means I am going to retrieve a page of three tours. Page equals one, which means its going to give me the second page of tours.
Sort equals title comma asc means I'm to sort by the title of the tour and ascending means it will be sorting alphabetically. So let's invoke that and so I have three tours returned. Now, let's use our Search 'Find by Tour Package' code with paging and sorting. The code equals CC for California Comm, we saw that before.
Now, we 're going to ask for a size of just two records, two tours. We're not providing the page number, so by default that will be zero. And sort equals title comma asc means it will also sort by title, alphabetically. And there are our two tours. Even more cool is that the response contains hyper-linked keys for navigating between pages.
The response contains the hrefs called First, Last, Pre, and Next. Front end developers love this. By using these hrefs, they don't have to calculate the last page number, nor keep state of the current page. We also see at the bottom, there is some meta data. If the client chooses not to use those links, the API also provides more page meta data at the bottom of the response packets, so they can calculate it themselves. In the past, we would have had to code all these features ourselves.
- Setting up the project
- Building, deploying, and launch the microservice
- Declaring Spring Data JPA repository interfaces
- Invoking repositories
- Using Spring Data query methods
- Exposing RESTful APIs with Spring Data REST
- Using the /search resource to invoke query methods
- Paging and sorting
- Declaring a new REST controller
- Creating HTTP methods for creating, reading, updating and deleting persistent data.