Pagination & Sorting
Fetching 500,000 records at once will crash your app. Use pages.
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".
- Pageable: An object that holds page number, size, and sorting details.
- 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.
- Zero-indexed: Page numbers start at 0.
page=0means the first page.
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);
}
}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
}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
Want to track your progress?
Log in to save your place and pick up where you left off.
Progress track karna chahte ho?
Login karo apni progress save karne ke liye aur jahan chhoda tha wahan se shuru karo.
Login