Pagination and Sorting
Return large datasets efficiently.
Pagination and Sorting is a free Spring Boot 4 Microservices & REST APIs lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Spring Boot 4 Microservices & REST APIs learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Why Paginate
Returning thousands of rows at once is slow and memory-heavy. Pagination fetches one page at a time.
Spring Data provides Pageable, Page, and Sort for this.
PagingAndSortingRepository
JpaRepository already includes paging and sorting support, so you get these methods for free.
public interface UserRepository
extends JpaRepository<User, Long> {
}Creating a Pageable
PageRequest.of(page, size) builds a request. Page numbers are zero-based.
Pageable pageable = PageRequest.of(0, 20);
Page<User> page = userRepository.findAll(pageable);The Page Object
A Page carries the content plus metadata.
List<User> content = page.getContent();
long total = page.getTotalElements();
int pages = page.getTotalPages();
boolean hasNext = page.hasNext();Sorting
Build a Sort and pass it alone or inside a PageRequest.
Sort sort = Sort.by("name").ascending();
List<User> sorted = userRepository.findAll(sort);Combining Sort and Paging
Pass a Sort into PageRequest.of.
Pageable pageable = PageRequest.of(
0, 20, Sort.by("name").descending());
Page<User> page = userRepository.findAll(pageable);Multiple Sort Orders
Chain sort orders for tie-breaking.
Sort sort = Sort.by(
Sort.Order.asc("lastName"),
Sort.Order.desc("createdAt"));Paging Derived Queries
Add a Pageable parameter to any derived query method.
Page<User> findByActiveTrue(Pageable pageable);Slice vs Page
A Slice knows only whether a next page exists, skipping the expensive COUNT query that Page runs. Use Slice for infinite scroll.
Slice<User> findByActiveTrue(Pageable pageable);Pageable from the Web Layer
Spring MVC can bind page, size, and sort request params into a Pageable automatically.
@GetMapping("/users")
public Page<User> list(Pageable pageable) {
return userRepository.findAll(pageable);
}
// GET /users?page=0&size=20&sort=name,descMapping Page Content to DTOs
Transform a Page's entities to DTOs while preserving pagination metadata with map.
Page<UserDto> dtos = page.map(
user -> new UserDto(user.getName(), user.getEmail()));Quick Check
Test your understanding of pagination.
Recap
You learned pagination and sorting:
PageRequest.of(page, size)— page is zero-basedPagecarries content plus totals;Sliceskips the countSort.by(...)for ordering- Web layer binds
page/size/sortinto aPageable
Frequently asked questions
Is the “Pagination and Sorting” lesson free?
Yes — the full text of “Pagination and Sorting” is free to read here on the web, and the Spring Boot 4 Microservices & REST APIs course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Spring Boot 4 Microservices & REST APIs course, upgrade to CoddyKit PRO.
What will I learn in “Pagination and Sorting”?
Return large datasets efficiently. You practise Spring Boot 4 Microservices & REST APIs with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Spring Boot 4 Microservices & REST APIs?
No prior experience is required. Spring Boot 4 Microservices & REST APIs on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Pagination and Sorting” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Spring Boot 4 Microservices & REST APIs lesson?
Yes. Every Spring Boot 4 Microservices & REST APIs lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Dynamic Queries with Specifications
- Projections and DTOs
- Auditing with @CreatedDate
- Pagination and Sorting