In Spring, Page<T> is the interface from Spring Data (org.springframework.data.domain.Page) that represents one slice of a larger result set together with the metadata needed to navigate it: the content of the current page, the page number, the page size, and the total number of elements and pages. You return it from a repository or service method when a client needs paginated data, such as a table with "next" and "previous" buttons.
A Page is produced by passing a Pageable (page index, size and sort order) to a repository method. Spring Data runs the data query plus a count query, so the caller can show "page 3 of 12" without loading everything into memory.
Page<T> extends Slice<T>, which extends Streamable<T>, so you can iterate it directly. The most useful methods are:
| Method | Returns |
|---|---|
getContent() | The List<T> of items on this page |
getNumber() | Zero-based index of the current page |
getSize() | The requested page size |
getNumberOfElements() | Items actually on this page (can be less than the size on the last page) |
getTotalElements() | Total rows matching the query |
getTotalPages() | Total number of pages |
hasNext() / hasPrevious() | Whether neighbouring pages exist |
isFirst() / isLast() | Position of this page |
getSort() | The sort applied |
map(Function) | A new Page with converted content, keeping the metadata |
The default implementation is PageImpl. You rarely construct it yourself except in tests or when paginating an in-memory list.
Any repository that extends PagingAndSortingRepository or JpaRepository supports paging. You can also add a Pageable parameter to a derived query method.
Calling it with PageRequest.of(0, 20, Sort.by("name")) returns the first 20 products of the category sorted by name:
Page numbers start at 0, not 1. This is the most common source of off-by-one bugs in UI code.
With Spring Data web support, Spring MVC resolves a Pageable argument from the query parameters page, size and sort, for example GET /products?page=2&size=10&sort=name,desc. Spring Boot enables this automatically when Spring Data is on the classpath.
Two practical points. First, map entities to DTOs with Page.map(...) rather than returning entities. Second, cap the page size: Spring Boot limits it with spring.data.web.pageable.max-page-size (default 2000) and uses spring.data.web.pageable.default-page-size (default 20) when the client sends nothing.
Serializing PageImpl directly produces a JSON shape that is not a stable, documented contract. Since Spring Data 3.3 (Spring Boot 3.3) you get a warning for this, and you have two supported options:
PagedModel, for example new PagedModel<>(page), which has a stable content and page structure.@EnableSpringDataWebSupport(pageSerializationMode = PageSerializationMode.VIA_DTO).The resulting JSON has the items under content and the numbers under page (size, number, totalElements, totalPages).
Choosing the return type controls how much work the database does.
| Return type | Count query | Knows total | Use when |
|---|---|---|---|
Page<T> | Yes | Yes | You show page numbers or a total |
Slice<T> | No (fetches size + 1 rows) | Only whether a next slice exists | "Load more" or infinite scroll |
List<T> with Pageable | No | No | You only need a window of rows |
The count query is the cost of Page. On very large tables it can be slower than the data query itself, so prefer Slice when the user does not need the total.
When the data is not in a database, build a PageImpl yourself:
The third constructor argument is the total, which is what makes getTotalPages() correct.
Zero-based. PageRequest.of(0, 10) is the first page. Convert in your API layer if clients expect page 1 to be the first.
Page runs an extra count query and exposes totals. Slice skips the count and only tells you whether another slice exists, which is cheaper.
Usually the count query or a missing index on the sort column. Check the generated SQL, index the sort and filter columns, and consider Slice or keyset pagination for deep pages.
Yes. Page belongs to Spring Data Commons, so it works with any module that supports paging, including Spring Data MongoDB and Spring Data JDBC.
Page<T> is the Spring Data type for one page of results plus pagination metadata. Request it with a Pageable, use map to return DTOs, and expose it through PagedModel or the DTO serialization mode for a stable JSON contract. Remember that page numbers start at 0, and use Slice when you do not need the total count.