In Spring Boot, Pageable is the request side of pagination and Page is the response side. Pageable tells a repository which page, how many items and which sort order you want. Page<T> is what comes back: the items of that page plus metadata such as total elements and total pages.
Together they let REST endpoints and repositories serve large datasets in small, sorted chunks without custom query code. Both come from Spring Data (org.springframework.data.domain).
Pageable carries the zero-based page number, the page size and a Sort. You can create one in code with PageRequest.of(...), or let Spring MVC build it from the page, size and sort request parameters.
A request to /products?page=0&size=20&sort=price,desc becomes a Pageable for the first page of 20 products ordered by price descending. Spring Boot registers the argument resolver automatically, and you can adjust it with spring.data.web.pageable.default-page-size and spring.data.web.pageable.max-page-size.
Page<T> extends Slice<T>, which extends Streamable<T>. It exposes:
| Method | Returns |
|---|---|
getContent() | Items on the current page |
getNumber() | Current zero-based page index |
getSize() | Requested page size |
getNumberOfElements() | Items actually on this page |
getTotalElements() | Total matching rows across all pages |
getTotalPages() | Total number of pages |
hasNext(), hasPrevious(), isFirst(), isLast() | Navigation flags |
map(Function) | Converts content, for example entity to DTO, keeping metadata |
Using map is the clean way to return DTOs without losing paging metadata.
When the return type is Page, Spring Data runs two queries: the data query with limit and offset, and a count query for the totals. If you do not need totals, declare the method to return Slice<Product>, which only checks whether a next slice exists and skips the count query. That is a good fit for infinite scrolling.
Returning a Page straight from a controller produces JSON with content, pageable, totalElements, totalPages and more. Since Spring Data 3.3, serializing PageImpl this way logs a warning, because that JSON structure is not guaranteed to stay stable. You have two supported options:
content plus page object:PagedModel<ProductDto> built with new PagedModel<>(page).Either way, expose DTOs rather than JPA entities.
Slice when totals are not displayed.ScrollPosition, Spring Data 3.1+) for very deep or very large datasets, since large offsets get slow.The following controller accepts Pageable, delegates to a service and returns a stable DTO model. It shows the whole flow from request parameters to response.
A request for /api/products?page=1&size=5 returns the second page of five items, with a page object holding size, number, totalElements and totalPages. In a @WebMvcTest, you can verify the binding by mocking the service and capturing the Pageable argument.
Pageable is the input describing the page you want. Page is the output holding the items and paging metadata.
No. Page extends Slice and adds total element and total page counts, at the cost of an extra count query.
Yes. They are part of the Spring Data Commons module and work with MongoDB, JDBC, Elasticsearch and other Spring Data stores.
Call page.map(dto mapping function). It returns a new Page with the same metadata.
Pageable describes the page you want and Page carries the result with its metadata. Accept Pageable in controllers, return Page, Slice or PagedModel of DTOs, cap page sizes, and choose the cheapest return type for your use case.