java.lang.IllegalArgumentException: Page index must not be less than zero!

Spring JIRA | Shelley J. Baker | 2 years ago
tip
Click on the to mark the solution that helps you, Samebug will learn from it.
As a community member, you’ll be rewarded for you help.
  1. 0

    When the {{PageableHandlerMethodArgumentResolver}} is configured with one-based index parameters: {code:java} resolver.setOneIndexedParameters(true); {code} and a zero or negative page parameter is provided, e.g. {{?page=0}} or {{?page=-1}}, an {{IllegalArgumentException}} is thrown, rather than falling back to the first page. {noformat} java.lang.IllegalArgumentException: Page index must not be less than zero! at org.springframework.data.domain.AbstractPageRequest.<init>(AbstractPageRequest.java:43) ~[spring-data-commons-1.10.0.RELEASE.jar:na] at org.springframework.data.domain.PageRequest.<init>(PageRequest.java:63) ~[spring-data-commons-1.10.0.RELEASE.jar:na] at org.springframework.data.web.PageableHandlerMethodArgumentResolver.resolveArgument(PageableHandlerMethodArgumentResolver.java:253) ~[spring-data-commons-1.10.0.RELEASE.jar:na] . . . {noformat} The page is initially set to the appropriate default page number of {{0}}, but then, one is subtracted from that value, resulting in a negative value, which is passed to the {{PageRequest}} constructor, resulting in the {{IllegalArgumentException}}: {code:java|title=PageableHandlerMethodArgumentResolver#resolveArgument} int page = StringUtils.hasText(pageString) ? parseAndApplyBoundaries(pageString, 0, Integer.MAX_VALUE) - (oneIndexedParameters ? 1 : 0) : defaultOrFallback.getPageNumber(); . . . return new PageRequest(page, pageSize, sort); {code} There may be multiple ways to solve this problem, but one approach could be to validate the lower bound after the {{page}} var is initially set, in a similar way as the {{pageSize}}: {code:java|title=PageableHandlerMethodArgumentResolver#resolveArgument} // Limit lower bound pageSize = pageSize < 1 ? defaultOrFallback.getPageSize() : pageSize; // TODO add similar lower bound limit for page param as well... {code}

    Spring JIRA | 2 years ago | Shelley J. Baker
    java.lang.IllegalArgumentException: Page index must not be less than zero!
  2. 0

    When the {{PageableHandlerMethodArgumentResolver}} is configured with one-based index parameters: {code:java} resolver.setOneIndexedParameters(true); {code} and a zero or negative page parameter is provided, e.g. {{?page=0}} or {{?page=-1}}, an {{IllegalArgumentException}} is thrown, rather than falling back to the first page. {noformat} java.lang.IllegalArgumentException: Page index must not be less than zero! at org.springframework.data.domain.AbstractPageRequest.<init>(AbstractPageRequest.java:43) ~[spring-data-commons-1.10.0.RELEASE.jar:na] at org.springframework.data.domain.PageRequest.<init>(PageRequest.java:63) ~[spring-data-commons-1.10.0.RELEASE.jar:na] at org.springframework.data.web.PageableHandlerMethodArgumentResolver.resolveArgument(PageableHandlerMethodArgumentResolver.java:253) ~[spring-data-commons-1.10.0.RELEASE.jar:na] . . . {noformat} The page is initially set to the appropriate default page number of {{0}}, but then, one is subtracted from that value, resulting in a negative value, which is passed to the {{PageRequest}} constructor, resulting in the {{IllegalArgumentException}}: {code:java|title=PageableHandlerMethodArgumentResolver#resolveArgument} int page = StringUtils.hasText(pageString) ? parseAndApplyBoundaries(pageString, 0, Integer.MAX_VALUE) - (oneIndexedParameters ? 1 : 0) : defaultOrFallback.getPageNumber(); . . . return new PageRequest(page, pageSize, sort); {code} There may be multiple ways to solve this problem, but one approach could be to validate the lower bound after the {{page}} var is initially set, in a similar way as the {{pageSize}}: {code:java|title=PageableHandlerMethodArgumentResolver#resolveArgument} // Limit lower bound pageSize = pageSize < 1 ? defaultOrFallback.getPageSize() : pageSize; // TODO add similar lower bound limit for page param as well... {code}

    Spring JIRA | 2 years ago | Shelley J. Baker
    java.lang.IllegalArgumentException: Page index must not be less than zero!

    Root Cause Analysis

    1. java.lang.IllegalArgumentException

      Page index must not be less than zero!

      at org.springframework.data.domain.AbstractPageRequest.<init>()
    2. Spring Data Core
      PageableHandlerMethodArgumentResolver.resolveArgument
      1. org.springframework.data.domain.AbstractPageRequest.<init>(AbstractPageRequest.java:43)[spring-data-commons-1.10.0.RELEASE.jar:na]
      2. org.springframework.data.domain.PageRequest.<init>(PageRequest.java:63)[spring-data-commons-1.10.0.RELEASE.jar:na]
      3. org.springframework.data.web.PageableHandlerMethodArgumentResolver.resolveArgument(PageableHandlerMethodArgumentResolver.java:253)[spring-data-commons-1.10.0.RELEASE.jar:na]
      3 frames