The Page interface in Spring Data is the return type you use when a repository query is paginated and the caller needs both the rows for one page and the metadata around them. It holds the content of the current page plus the total number of elements, the total number of pages, the page number and the page size. Because it knows the total, it makes a count query, which is the main difference from Slice and plain List.
In current Spring Data JPA (Spring Boot 3.x, Spring Data 3.x), you request a page by passing a Pageable to a repository method and receive a Page<T> back.
org.springframework.data.domain.Page<T> extends Slice<T>, which in turn extends Streamable<T>. A Page is a read-only snapshot of one chunk of a larger result set. Spring Data creates it for you (as a PageImpl) when a repository method declares Page<T> as its return type and takes a Pageable argument.
A page contains:
getContent()),getPageable() and getSort()),getTotalElements()) and the derived page count (getTotalPages()).Declare the return type on a repository that extends JpaRepository (which already includes PagingAndSortingRepository methods such as findAll(Pageable)):
Call it with a PageRequest:
Page numbers are zero-based, so page = 0 returns the first page. Spring Data runs the select with LIMIT/OFFSET (or the database equivalent) and a second count query to fill in the totals.
| Method | Meaning |
|---|---|
getContent() | The List<T> of items on this page |
getNumber() | Zero-based index of this page |
getSize() | Requested page size |
getNumberOfElements() | Items actually on this page (can be smaller than the size on the last page) |
getTotalElements() | Total rows matching the query across all pages |
getTotalPages() | Number of pages for the requested size |
hasNext(), hasPrevious() | Whether neighbouring pages exist |
isFirst(), isLast() | Whether this is the first or last page |
hasContent(), isEmpty() | Whether this page has any items |
The return type decides how much work the database does.
| Return type | Count query | Knows total | Typical use |
|---|---|---|---|
Page<T> | Yes | Yes | Numbered pagination UIs, admin tables |
Slice<T> | No (fetches one extra row) | Only whether a next slice exists | "Load more" and infinite scroll |
List<T> | No | No | Simple limits, internal batch jobs |
On a table with millions of rows, the count query can dominate the cost. If you never show a total, return Slice<T>. For expensive custom queries you can also supply a cheaper countQuery in @Query.
Do not expose JPA entities directly from a controller. Page.map() converts each element and keeps the paging metadata:
Spring Data's web support resolves a Pageable from the page, size and sort request parameters, so the controller stays small:
A request such as GET /products?page=0&size=5&sort=name,asc returns the page. By default the page size is 20 and is capped by spring.data.web.pageable.max-page-size, which protects the API from clients asking for huge pages.
Returning the raw PageImpl as JSON works, but its JSON shape is not a stable, documented contract. Since Spring Data 3.3 a warning is logged for this, and you can opt in to a stable DTO shape with @EnableSpringDataWebSupport(pageSerializationMode = PageSerializationMode.VIA_DTO), or wrap results in PagedModel. For a public API, a small response record of your own is also a good choice.
When the data does not come from a repository, such as an in-memory list or an external service, create a PageImpl yourself:
It is 0. PageRequest.of(0, 10) is the first page. If your clients send one-based numbers, subtract one yourself or set spring.data.web.pageable.one-indexed-parameters=true for the web layer.
Yes. A Page return type triggers a separate count query so that getTotalElements() and getTotalPages() are accurate. Use Slice to avoid it.
Yes. Put the Sort inside the PageRequest, or let the controller bind sort=name,asc from the request into the Pageable.
A Page is an immutable snapshot, so sharing it is safe. For JSON output, prefer mapping to your own DTO or PagedModel rather than exposing PageImpl directly.
The Page interface carries one page of results together with the metadata needed to build pagination controls: total elements, total pages, page number and navigation flags. It costs one extra count query, so choose Slice when you do not need totals. Combine it with PageRequest, Sort and map() to keep controllers thin and responses free of entities.