Chapter 4.5☕ 14 min read

Pagination & Sorting

Fetching 500,000 records at once will crash your app. Use pages.

01The Concept: Chunking Data

Imagine your database has 500,000 books. If a user calls GET /api/books, fetching all 500,000 records at once will crash your Java app and freeze the database. We must split the data into pages (Pagination) and order it (Sorting).

The Charminar Crowd Control Analogy:

During festivals, Charminar gets incredibly crowded. Police don't let all 10,000 people enter the monument at the same time. They let in 50 people at a time (Pagination), and they line them up by height or ticket number (Sorting).

Spring Data JPA provides a Pageable interface that does exactly this. You just pass it "Give me page 0, size 10, sorted by title".

02Technical Explanation
  1. Pageable: An object that holds page number, size, and sorting details.
  2. Page<T>: The return type. It contains the list of data for that page, plus metadata like total pages, total elements, and current page number.
  3. Zero-indexed: Page numbers start at 0. page=0 means the first page.
03Full Working Code: Pageable in Action

Spring Boot automatically maps URL query parameters (?page=0&size=5&sort=title,asc) into a Pageable object!

package com.devinhyderabad;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}

interface BookRepository extends JpaRepository<Book, Long> {
// JpaRepository already has findAll(Pageable) built-in!
}

@RestController
@RequestMapping("/api/books")
class BookController {

private final BookRepository bookRepository;
public BookController(BookRepository bookRepository) { this.bookRepository = bookRepository; }

// Spring automatically maps ?page=0&size=5&sort=title,asc to the Pageable object
@GetMapping
public Page<Book> getBooks(Pageable pageable) {
// Returns a chunk of books with total count metadata
return bookRepository.findAll(pageable);
}
}
04JSON Response Example

Test it in your browser: http://localhost:8080/api/books?page=0&size=3&sort=title,desc

The JSON response will look like this:

{
"content": [ ... 3 books ... ],
"pageable": { "pageNumber": 0, "pageSize": 3 },
"totalElements": 10,
"totalPages": 4,
"last": false
}
05Why It Matters / Interview Note

Interview Question: "How do you implement pagination in a Spring Boot REST API?"

Answer: "I add a Pageable parameter to my controller method. Spring Boot automatically resolves query parameters like page, size, and sort into this object. I pass it to repository.findAll(pageable), which returns a Page<T>. This object contains the data chunk and metadata like total pages, which I send back to the frontend."

Enterprise Note: Frontend frameworks like Angular have table components (like Angular Material Tables) that directly consume Spring Boot's Page<T> JSON format. The totalElements field is exactly what the frontend needs to draw the page numbers (1, 2, 3, 4) at the bottom of the table.

Key Takeaways

  • ✅ Pageable interface encapsulates page number, size, and sort details
  • ✅ Page returns data plus metadata (totalPages, totalElements)
  • ✅ Spring Boot auto-resolves query params ?page, ?size, ?sort into Pageable
  • ✅ Page numbers are zero-indexed — page=0 is the first page