Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Pagination and sorting with Pageable in Spring Boot
📖 Scenario: You are building a simple Spring Boot application to manage a list of books in a library. The library has many books, so you want to show the books page by page and allow sorting by title or author.
🎯 Goal: Build a Spring Boot REST controller that returns a paginated and sorted list of books using Spring Data's Pageable interface.
📋 What You'll Learn
Create a list of books as initial data
Add a Pageable parameter to the controller method
Use the Pageable to return a paginated and sorted page of books
Complete the REST controller to handle requests with pagination and sorting
💡 Why This Matters
🌍 Real World
Pagination and sorting are common in web applications to improve user experience by loading data in small chunks and allowing users to order data by different fields.
💼 Career
Understanding how to use Spring Data's Pageable interface is essential for backend developers working with Spring Boot to build scalable and user-friendly APIs.
Progress0 / 4 steps
1
Create initial book data
Create a List<Book> called books with these exact entries: new Book(1L, "Spring Boot Basics", "John Doe"), new Book(2L, "Java Fundamentals", "Jane Smith"), new Book(3L, "REST APIs", "Alice Johnson"), new Book(4L, "Microservices", "Bob Brown").
Spring Boot
Hint
Use List.of() to create an immutable list of Book objects with the exact data.
2
Add Pageable parameter to controller method
Add a public method called getBooks that takes a Pageable pageable parameter and returns a Page<Book>. Do not implement the method body yet.
Spring Boot
Hint
Define the method with the exact name getBooks, return type Page<Book>, and a single parameter Pageable pageable.
3
Implement pagination and sorting logic
Inside the getBooks method, create a PageImpl<Book> object that returns a sublist of books based on pageable.getPageNumber() and pageable.getPageSize(). Use pageable.getSort() to sort the list by title or author before paging.
Spring Boot
Hint
Use a stream to sort the books by the properties in pageable.getSort(). Then calculate the start and end indexes for the page and create a PageImpl with the sublist.
4
Complete the REST controller with annotations
Add the @RestController annotation to the class and @GetMapping("/books") to the getBooks method. Add @RequestParam to accept page, size, and sort parameters automatically via Pageable.
Spring Boot
Hint
Add @RestController above the class and @GetMapping("/books") above the getBooks method to make it a REST endpoint that accepts pagination and sorting parameters.
Practice
(1/5)
1. What is the main purpose of using Pageable in Spring Boot?
easy
A. To fetch data in small chunks with optional sorting
B. To connect to the database directly
C. To write SQL queries manually
D. To handle user authentication
Solution
Step 1: Understand Pageable's role
Pageable is used to request data in pages, not all at once.
Step 2: Recognize sorting feature
It also supports sorting data by fields while fetching pages.
Final Answer:
To fetch data in small chunks with optional sorting -> Option A
Quick Check:
Pageable = Pagination + Sorting [OK]
Hint: Pageable means small pages + sorting [OK]
Common Mistakes:
Thinking Pageable connects to database
Confusing Pageable with authentication
Assuming Pageable writes SQL queries
2. Which of the following is the correct way to create a Pageable object for page 2, size 5, sorted by "name" ascending?
easy
A. PageRequest.of(1, 5, Sort.by("name"))
B. PageRequest.of(2, 5, Sort.asc("name"))
C. PageRequest.of(2, 5, Sort.by("name"))
D. PageRequest.create(2, 5, Sort.by("name"))
Solution
Step 1: Understand zero-based page index
Page numbers start at 0, so page 2 means index 1.
Step 2: Check method and sorting syntax
Use PageRequest.of(page, size, Sort.by("field")) for ascending sort.
Final Answer:
PageRequest.of(1, 5, Sort.by("name")) -> Option A
Quick Check:
Page index zero-based + Sort.by correct [OK]
Hint: Page index starts at 0, so page 2 is index 1 [OK]
Common Mistakes:
Using page number directly instead of zero-based index
Using non-existent methods like Sort.asc
Using PageRequest.create instead of PageRequest.of
3. Given this repository method call: repository.findAll(PageRequest.of(0, 3, Sort.by("age"))) What will be the result?
medium
A. Error because Sort direction is missing
B. First 3 records sorted by age descending
C. All records without sorting
D. First 3 records sorted by age ascending
Solution
Step 1: Analyze PageRequest parameters
Page 0 means first page, size 3 means 3 records, Sort.by("age") defaults to ascending.
Step 2: Understand repository behavior
findAll with Pageable returns that page of sorted data.
Final Answer:
First 3 records sorted by age ascending -> Option D