How to configure Spring boot pagination starting from page 1, not 0

前端 未结 4 1572
别那么骄傲
别那么骄傲 2021-02-04 03:01

boot(1.4.0) \"Pageable\" for pagination.It works fine without any issue.But by default the page value starts from \"0\" but in the front-end the page value starts from \"1\". So

4条回答
  •  别跟我提以往
    2021-02-04 04:03

    Spring Boot will be using Spring Data under the covers.

    The Spring Data class you need to configure is the following:

    org.springframework.data.web.PageableHandlerMethodArgumentResolver

    and in particular the following method:

    http://docs.spring.io/spring-data/commons/docs/current/api/org/springframework/data/web/PageableHandlerMethodArgumentResolver.html#setOneIndexedParameters-boolean-

    This will allow you to use you current UI paging as is i.e. with first page = 1.

    In a Boot application I think the config may look something like:

    @Configuration
    public class WebConfig extends WebMvcConfigurerAdapter {
    
    
        @Override
        public void addArgumentResolvers(List argumentResolvers) {
            PageableHandlerMethodArgumentResolver resolver = new PageableHandlerMethodArgumentResolver();
            resolver.setOneIndexedParameters(true);
            argumentResolvers.add(resolver);
            super.addArgumentResolvers(argumentResolvers);
        }
    }
    

提交回复
热议问题