Spring Boot services · Chapter 18 of 41

Pagination and sorting

By the end of this chapter you can answer a list one page at a time, sorted the way the client asks, with what a Next button needs, and explain offset and keyset paging. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Data
  • 5 tasks
  • Spring Boot
  • In the all labs plan

The first 2 chapters of Spring Boot services are free. This one comes with the all labs plan.

Why you need this

Head office opens the orders screen and the browser freezes: it got every order since launch in one response. Meera wants five orders and a Next button; the station screens want only their own station's orders, biggest first.

findAll() returns the whole table. Cutting the list in Java still loads every row, and the client cannot tell how many pages there are. The database can hand back one slice and count the rest.

What you build: 5 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    OrderController.java: one page of orders with its metadata

    Write PageResponse.from(page) so it copies the Page's content, page number, page size, total elements and total pages. Then make GET /orders answer 200 with the page of orders the Pageable asks for, each mapped to an OrderView (id, station, item, amount), as a PageResponse.

    GET /orders?page=1&size=5&sort=id gives Ginger chai, Vada pav, Poha, Parle-G and Kachori, with page 1, size 5, totalElements 12 and totalPages 3; page 2 gives Elaichi chai and Veg thali. Pages count from 0, and Spring Data's own fields (such as pageable) must not appear in the answer.

  2. Task 2

    OrderController.java: default to the first 5 orders by id

    Make GET /orders with no parameters answer the first page of 5 orders sorted by id: page 0, size 5, totalPages 3 for the 12 orders, starting Masala chai, Masala chai, Bun maska, Cutting chai, Samosa.

    Parameters the client does send still win. Needs task 1.

  3. Task 3

    OrderController.java: page one station's orders

    Make GET /stations/{code}/orders answer a PageResponse of only that station's orders, paged and sorted as the query string asks, with that station's totals: SBC with size=3&sort=amount,desc gives amounts 120, 90 and 75, totalElements 7 and totalPages 3; NDLS with page=1&size=3&sort=amount gives Masala chai and Veg thali, totalElements 5.

    A station with no orders (CSMT) answers 200 with an empty content, totalElements 0 and totalPages 0. Use a derived query in OrderRepository. Needs task 1.

  4. Task 4

    OrderController.java: the biggest orders with a PageRequest

    Make GET /orders/biggest?count=3 answer 200 with a plain JSON list (no page metadata) of the count orders with the largest amounts, largest first, as OrderViews: Bun maska, Veg thali, Ginger chai; count=1 gives the 120 order alone.

    Only those count orders may be loaded: the check expects 3 orders loaded for count=3, so loading every order and sorting in Java fails.

  5. Task 5 · interview question

    Interview: page with a cursor (keyset) instead of an offset

    Make GET /orders/after/{id}?size=4 answer a CursorPage: content holds the next size orders whose id is greater than the cursor id, in id order, as OrderViews, and next is the id of the last of them when the page is full (size orders), or null when it holds fewer. Load only those orders (4 for size=4).

    Deleting an order before the cursor must not change the page. Be ready to explain why an offset page skips rows when earlier rows are deleted, and why a keyset page stays fast deep in the list.

What you will learn

  • Pageable: page, size and sort, read from the query string.
  • Page: one slice of rows plus the total, from two queries.
  • PageRequest and Sort: a page built in code, for "the top few".
  • Offset or keyset: two ways to page, and why one skips rows (the interview question).

More in Data

Build "Pagination and sorting" yourself

Tapri Cloud: the counters’ Java grows into a Spring Boot service with a database. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of Spring Boot services are free. This one comes with the all labs plan.