OrderHub Day 6: the list endpoint that returns everything is a time bomb. With 10 orders it's fine; with 10 million it OOMs the server and times out the client. Today: pagination, sorting, and filtering โ done the Spring Data way.
๐ Try the paged/sorted/filtered list: https://dev48v.infy.uk/orderhub/day6-pagination.html
Pageable + Page
Spring Data hands you this almost for free. The controller accepts a Pageable, so GET /orders?page=1&size=20&sort=createdAt,desc is parsed automatically; the repository returns a Page<Order> โ a slice of content plus metadata (totalElements, totalPages, number).
@GetMapping
PagedResponse<OrderResponse> list(
@RequestParam(required=false) String status,
@PageableDefault(size=20, sort="createdAt") Pageable pageable) { ... }
Filtering behind the port
OrderHub keeps its repository port abstraction: I added search(status, pageable) to the interface and implemented it in both backends โ a derived findByStatus(status, pageable) for JPA, and a manual filter+sort+slice for the in-memory one. The service and controller don't care which is active.
Three production habits
-
Cap the page size (
@PageableDefault+ a max) so nobody requestssize=1000000โ a cheap DoS. - Return DTOs, not entities, even in a page.
- Know that offset paging gets slow deep in huge tables โ keyset/cursor paging scales better (a later day).
๐จ Full walkthrough (Pageable โ Page โ @PageableDefault โ findByStatus โ PagedResponse) on the page: https://dev48v.infy.uk/orderhub/day6-pagination.html
OrderHub โ a production-grade Spring Boot backend, one feature a day.
๐ https://dev48v.infy.uk ยท Code: https://github.com/dev48v/order-hub-from-zero
Top comments (0)