Quick revision
The short answer
Spring resolves the Pageable from the page, size and sort request parameters (page is zero-based) and Jackson serializes the returned PageImpl with all its internals: content, a nested pageable object, sort flags, totals and several booleans. That structure is not a stable contract and can change between versions, so Spring Data 3.3 and later warns about it and offers @EnableSpringDataWebSupport(pageSerializationMode = VIA_DTO), which renders a stable PagedModel. The alternative is our own page DTO, and in either case we cap the page size.
What to remember
In this answer, we saw that Pageable comes from page, size and sort parameters with a zero-based page, and that returning Page<T> directly serializes PageImpl's internal structure, which is not stable across versions. Spring Data 3.3 and later warns about this and offers the VIA_DTO mode with a stable PagedModel shape, and we can also map to our own page DTO. Finally, we capped the page size and restricted sort properties.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Spring resolves the Pageable from the page, size and sort request parameters (page is zero-based) and Jackson serializes the returned PageImpl with all its internals: content, a nested pageable object, sort flags, totals and several booleans. That structure is not a stable contract and can change between versions, so Spring Data 3.3 and later warns about it and offers @EnableSpringDataWebSupport(pageSerializationMode = VIA_DTO), which renders a stable PagedModel. The alternative is our own page DTO, and in either case we cap the page size.
Why this question is asked
Returning Page<T> straight from a controller is one of the most common shortcuts in Spring applications, because it works in a single line. Interviewers ask about it because the JSON it produces becomes a public contract by accident, and the fix shows whether a candidate keeps up with Spring Data's changes. In this answer, we will recap how Pageable is resolved, look at the JSON that comes out, see why it is a problem, apply the two fixes, and protect the endpoint from huge pages and unexpected sort fields.
1A Quick Recap of Pageable
Pageable is Spring Data's description of which page we want: a page number, a page size and a sort. When a controller method declares a Pageable parameter, Spring Data's web support builds it from request parameters. By default the parameters are page, size and sort, page is zero-based, and the default size is 20.
The repository runs one query for the slice of rows and, for a Page, a second count query to compute the total. The Page object it returns is a PageImpl, an implementation class that holds the content and the paging information.
2What the JSON Looks Like
Jackson knows nothing about pages; it serializes PageImpl by reading its getters. The result exposes every public property of the class and of the nested Pageable and Sort objects.
Notice how much of this is noise: paging information appears twice, sort flags appear twice, and fields such as offset and unpaged describe Spring's internal objects, not anything a client needs.
3Why This Is a Problem
The JSON shape is decided by the getters of an internal class, not by us. If a Spring Data release adds, renames or removes a getter on PageImpl, Pageable or Sort, our API response changes without any change in our code, and clients that parse it break. Our API contract has become an accident of a library's implementation.
Because this has caught many teams, Spring Data 3.3 started logging a warning when a PageImpl is serialized as is, telling us the resulting JSON structure is not guaranteed to be stable. The warning is the framework's way of saying that this one-line shortcut should not reach production APIs.
4Fix 1: Let Spring Data Render a Stable PagedModel
Spring Data 3.3 also added a switch that tells it to convert every Page into a PagedModel, a small DTO with a deliberately stable shape, before Jackson sees it.
| Aspect | PageImpl serialized directly | PagedModel (VIA_DTO) |
|---|---|---|
| Who decides the shape | getters of internal classes | a DTO designed as a contract |
| Paging fields | duplicated in pageable and top level | one page object |
| Stable across upgrades | not guaranteed, warning logged | intended to be stable |
| Code change in controller | none | none, only configuration |
The controller does not change; it still returns Page<ProductDto>. The shape is now small and documented, and future Spring Data releases promise to keep it. Check whether your Spring Data version changes the default mode, since newer releases may make VIA_DTO the default.
5Fix 2: Map to Our Own Page DTO
If the API guideline already defines a page envelope, we map to it ourselves. This gives us full control over field names and lets us leave out totals where they are expensive.
The JSON now belongs to us, and it is the same across every service that uses the shared record.
6Protecting the Endpoint
Two more details matter in production. First, a client can send size=100000 and make the database return a huge result. Spring Data caps the size with spring.data.web.pageable.max-page-size, which defaults to 2000; for most APIs we set it much lower, for example 100. We can also set the default size with spring.data.web.pageable.default-page-size, and spring.data.web.pageable.one-indexed-parameters=true if the API should count pages from 1.
Second, the sort parameter accepts any property name, and an unknown one makes the repository throw an exception that may surface as a 500 error, while a valid but unindexed one can cause a slow full sort. So we check sort properties against an allow list, as the FAQ below shows.
7Follow-up questions
How do you whitelist the properties a client may sort by?
We check pageable.getSort() in the controller or in a small reusable resolver before calling the repository. For each Sort.Order we compare order.getProperty() with a set such as Set.of("name", "price", "createdAt"), ideally only indexed columns, and if any property is not in the set we throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "cannot sort by " + property). Rejecting with 400 is better than silently ignoring the parameter, because the client learns immediately that its request was not honoured. Mapping public sort names to entity properties also stops clients from sorting on internal fields.
Why does a Page run two queries?
Page includes the total number of elements, which needs a count query in addition to the query for the slice. If the client does not need the total, returning a Slice skips the count and only checks whether a next page exists, which is much cheaper on large tables.
Should the controller return entities inside the page?
No. Mapping to a DTO such as ProductDto keeps the entity's internal fields and lazy associations out of the JSON, for the same reason we do not want PageImpl's internals in it.
8Conclusion
In this answer, we saw that Pageable comes from page, size and sort parameters with a zero-based page, and that returning Page<T> directly serializes PageImpl's internal structure, which is not stable across versions. Spring Data 3.3 and later warns about this and offers the VIA_DTO mode with a stable PagedModel shape, and we can also map to our own page DTO. Finally, we capped the page size and restricted sort properties.
Open with
When a controller takes a Pageable, Spring Data builds it from the page, size and sort parameters, with page zero-based and a default size of 20. If we return the Page directly, Jackson serializes PageImpl through its getters, so the JSON contains content plus a nested pageable object, duplicated sort flags, totals and booleans like first, last and empty.
Then, if they want more
The problem is that this shape is decided by internal classes, not by us, so a Spring Data upgrade can change our API contract without any change in our code. That is why Spring Data 3.3 logs a warning when PageImpl is serialized as is.
The fix it offers is EnableSpringDataWebSupport with pageSerializationMode VIA_DTO, or the equivalent Boot property, which converts the page into a PagedModel with just content and a page object holding size, number, totalElements and totalPages. Alternatively I would map to our own page record with items, page, size and total, especially if the company has a standard envelope.
I would also set spring.data.web.pageable.max-page-size to something like 100, since the default cap is 2000, and validate sort properties against an allow list of indexed fields, rejecting others with a 400.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Spring Boot 3.3
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Spring Framework reference, Spring Web MVC
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "A Spring MVC endpoint returns Page<ProductDto> directly from a controller using Spring...": https://scrivio-interview-library.netlify.app/spring-boot/rest-api-design/badc90cb51/