Spring Boot Tutorial 0/110 lessons ~6 min read Lesson 33

    Pagination & Sorting

    Returning unbounded result sets is how junior backends become production incidents.

    Course progress0%
    Focus
    3 guided sections
    Practice signal
    Examples included
    Career prep
    Foundation builder

    Introduction

    Returning unbounded result sets is how junior backends become production incidents. Spring Data's Pageable makes paging and sorting trivial.

    Informative example

    ts
    // Repository
    Page<Order> findByStatus(OrderStatus status, Pageable pageable);
    // Controller
    @GetMapping
    public Page<OrderDto> list(
    @RequestParam(defaultValue = "PENDING") OrderStatus status,
    @PageableDefault(size = 20, sort = "createdAt", direction = Sort.Direction.DESC) Pageable page
    ) {
    return repo.findByStatus(status, page).map(this::toDto);
    }
    # request
    GET /api/orders?status=PAID&page=2&size=50&sort=totalCents,desc

    Best practices

    • Cap max size with a custom argument resolver: hard limit 100.
    • Prefer keyset (cursor) pagination for large datasets — offset gets slow at high pages.
    • Always sort by a unique field; ties + paging = duplicate rows across pages.
    Ready to mark this lesson complete?Track your journey across the entire course.