Spring Data web pagination "page" parameter not working

11,608

Solution 1

the page parameter is actually a bit non-intuitive - page.page and not page, changing to page.page should get things to work.

Solution 2

Looking into the PageableArgumentResolver I found that prefix and separator are configurable, so you can configure the class not to have it.

public class PageableArgumentResolver implements WebArgumentResolver {

    private static final Pageable DEFAULT_PAGE_REQUEST = new PageRequest(0, 10);
    private static final String DEFAULT_PREFIX = "page";
    private static final String DEFAULT_SEPARATOR = ".";

in my @Configuration class I did something different to achieve using page, size,sort and sortDir as default.

@Override
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
    PageableArgumentResolver resolver =  new PageableArgumentResolver();
    resolver.setPrefix("");
    resolver.setSeparator("");
    argumentResolvers.add(new ServletWebArgumentResolverAdapter(resolver));
}

Now this works

http://:8080/myapp/list?page=14&size=42

Share:
11,608
Admin
Author by

Admin

Updated on June 19, 2022

Comments

  • Admin
    Admin almost 2 years

    I'm trying to get Spring Data's web pagination working. It's described here:

    http://static.springsource.org/spring-data/data-jpa/docs/current/reference/html/repositories.html#web-pagination

    Here's my Java (Spring Web MVC @Controller handler method):

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public String list(
        @PageableDefaults(value = 50, pageNumber = 0) Pageable pageable,
        Model model) {
    
        log.debug("Params: pageNumber={}, pageSize={}",
            pageable.getPageNumber(), pageable.getPageSize());
    
        ...
    }
    

    And here's my Spring configuration:

    <mvc:annotation-driven>
        <mvc:argument-resolvers>
            <bean class="org.springframework.data.web.PageableArgumentResolver" />
        </mvc:argument-resolvers>
    </mvc:annotation-driven>
    

    (It appears that the configuration above is the way to do this now; the configuration approach described in the link is deprecated.)

    When I actually try to control the pagination using the page and page.size parameters, the latter works just fine, but the former doesn't. For example, if I hit

    http://localhost:8080/myapp/list?page=14&page.size=42
    

    the log output is

    Params: pageNumber=0, pageSize=42
    

    So I know that the argument resolver is kicking in, but not sure why it's not resolving the page number. I've tried a bunch of other param names (e.g. page.number, pageNumber, page.num, etc.) and none of them work.

    Is this working for anybody else?