4. Paging and Sorting

4.1 Paging

Rather than return everything from a large result set, Spring Data REST recognizes some URL parameters that will influence the page size and starting page number. To add paging support to your Repositories, you need to extend the PagingAndSortingRepository<T,ID> interface rather than the basic CrudRepository<T,ID> interface. This adds methods that accept a Pageable to control the number and page of results returned.

public Page findAll(Pageable pageable);

If you extend PagingAndSortingRepository<T,ID> and access the list of all entities, you'll get links to the first 20 entities. To set the page size to any other number, add a limit parameter:


To get paging in your query methods, you must change the signature of your query methods to accept a Pageable as a parameter and return a Page<T> rather than a List<T>. Otherwise, you won't get any paging information in the JSON and specifying the query parameters that control paging will have no effect.

By default, the URL query parameters recognized are page, to specify page number limit, to specify how many results to return on a page, and sort to specify the query method parameter on which to sort. To change the names of the query parameters, simply call the appropriate method on RepositoryRestConfiguration and give it the text you would like to use for the query parameter. The following, for example, would set the paging parameter to p, the limit parameter to l, and the sort parameter to q:

@Override protected void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {

The URL to use these parameters would then be changed to:


4.1.1 Previous and Next Links

Each paged response will return links to the previous and next pages of results based on the current page. If you are currently at the first page of results, however, no "previous" link will be rendered. The same is true for the last page of results: no "next" link will be rendered if you are on the last page of results. The "rel" value of the link will end with ".next" for next links and ".prev" for previous links.

  "rel" : "people.next",
  "href" : "http://localhost:8080/people?page=2&limit=20"

4.2 Sorting

Spring Data REST also recognizes sorting parameters that will use the Repository sorting support.

To have your results sorted on a particular property, add a sort URL parameter with the name of the property you want to sort the results on. You can control the direction of the sort by specifying a URL parameter composed of the property name plus .dir and setting that value to either asc ordesc. The following would use the findByNameStartsWith query method defined on the PersonRepository for all Person entities with names starting with the letter "K" and add sort data that orders the results on the name property in descending order:

        curl -v http://localhost:8080/people/search/nameStartsWith?name=K&sort=name&name.dir=desc