Why Does Pageable Return 0 Instead of Error When Page Number Exceeds Int Range?
Analysis of why Spring Boot Pageable converts page values exceeding int range to 0 and its internal behavior.
Environment
spring boot 3.2.9,jdk17
Problem Description
While implementing pagination for large datasets, I discovered an interesting behavior (skip if you already know this).
When usingPageableas a parameter, even when inputting a very large number exceeding theintrange (e.g.,10 billion) for thepagevalue,
contrary to expectations (and different from custom implementation), no error occurred and it returned the first page (pageNo = 0).
This was different from when I implemented it directly using @RequestParam.
In this article, I’ll examine the cause and how it works internally.
What is Pagination with Pageable?
In general, most web bulletin boards don’t show all posts at once but provide them page by page.
You can also set sorting options to prioritize the information you want to see. Pagination is about delivering information based on the sorting method, page size, and which page number is requested.
Developers can implement this directly, but Spring Data provides an interface called Pageable for convenient use.
Reproducing the Situation
Querying with Pageable
As shown in the image below, I simply reproduced a method that receives Pageable as a parameter for querying.
In this case, when requesting a query with the query string page set to 10 billion, contrary to expectations, it queries the 0th page.
Fetching the first page
Custom Implementation with @RequestParam
I found this quite interesting… because when I implemented it directly using RequestParam without Pageable, I remembered it returning a 400 bad request error.
So I reproduced it again below, and as expected, the image shows that HandlerExceptionResolver threw an error saying it failed to convert the value to int.
Why Doesn’t an Error Occur?
To find the cause of this behavior, I debugged the code.
I discovered that in Spring, when receiving a Pageable object directly as a parameter, the PageableHandlerMethodArgumentResolverSupport class handles it.
This class is responsible for converting request parameters to a Pageable object through the getPageable method.
During this process, the parseAndApplyBoundaries method is called, which is where the cause of this behavior lies.
The parseAndApplyBoundaries method catches the NumberFormatException that occurs when the page number is too large (exceeding 2.1 billion),
and because of this, it’s designed to return the first page (0) when accessing actual data.
As a result, even when inputting a value exceeding the int range, no error occurs and the first page is returned.
Conclusion
Understanding this behavior when using Pageable will make it easier to handle similar situations clearly. I hope this article helps understand the edge cases where Pageable behaves differently from custom implementations and contributes to better pagination implementation.







