Paging, Sorting & Projections
Return one page at a time with Pageable, sort results, and fetch only the columns you need with projections.
On this page
A library might hold a hundred thousand books. findAll() returning every one in a
single response is slow, memory-hungry, and useless to a client that shows twenty at a
time. Spring Data has first-class support for returning one page at a time,
sorted, fetching only the fields you need.
Paging with Pageable
Add a Pageable parameter and return a Page:
interface BookRepository extends JpaRepository<Book, Long> {
Page<Book> findByAuthor(String author, Pageable pageable);
}Page<Book> page = repository.findByAuthor(
"Joshua Bloch",
PageRequest.of(0, 20, Sort.by("title")) // page 0, 20 per page, sorted by title
);
page.getContent(); // the 20 books on this page
page.getTotalElements(); // total matching rows
page.getTotalPages(); // total pagesA Page carries the slice and the metadata a UI needs to render "Page 1 of 47."
Spring Data runs an efficient LIMIT/OFFSET query plus a count query for you.
Let Spring bind the page from the request
In a controller, accept Pageable directly and Spring binds it from query parameters:
GET /api/books?page=2&size=20&sort=title,asc. No manual parsing - the client controls
paging and sorting through the URL.
Nobody wants a web search that returns ten million links on one page. It gives you
page one - ten results, sorted by relevance - plus "Next" and a total count. A JPA
Page is exactly that: a manageable slice of the data with the navigation metadata
attached, so your API and UI stay fast no matter how big the table grows.
Projections: fetch only what you need
Sometimes you don't need the whole entity - a dropdown just needs each book's title and ISBN. A projection interface tells Spring Data to select only those columns:
interface BookSummary { // a projection
String getIsbn();
String getTitle();
}
interface BookRepository extends JpaRepository<Book, Long> {
List<BookSummary> findByAuthor(String author); // SELECT isbn, title …
}Less data leaves the database, less memory is used, and the response is leaner - a straightforward performance win when you don't need every field.
BookVault's catalog grows to 100,000 books and a client wants to display them 25 at a time, sorted by title, showing only each book's title and ISBN in the list view. Which two Spring Data features do you combine, and what would the request URL look like?
What does returning a Page<Book> (with a Pageable) give you over returning List<Book>?
Key takeaways
- Return Page<T> with a Pageable parameter to fetch one page at a time, with an efficient LIMIT/OFFSET query plus a count.
- A Page carries both the slice and metadata (total elements/pages) a UI needs.
- Controllers can accept Pageable directly and Spring binds it from ?page=&size=&sort= query params.
- Projections (interfaces exposing a subset of getters) fetch only the columns you need, reducing data transfer and memory.
- Combine paging + projections to serve large catalogs quickly.