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

前端 未结 4 1583
别那么骄傲
别那么骄傲 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:07

    To get better result, you need to extend RepositoryRestMvcConfiguration, like below

    @Configuration
       public class RespositoryConfiguration extends RepositoryRestMvcConfiguration {
    
        @Override
        @Bean
        public HateoasPageableHandlerMethodArgumentResolver pageableResolver() {
    
            HateoasPageableHandlerMethodArgumentResolver resolver = super.pageableResolver();
            resolver.setOneIndexedParameters(true);
            return resolver;
        }
    }
    

    Then you shall get all the links and pagination information correct except 'number', which is one less. See the result for page=1,

      "_links": {
        "first": {
          "href": "http://localhost:8080/user/user?page=1&size=5"
        },
        "self": {
          "href": "http://localhost:8080/user/user"
        },
        "next": {
          "href": "http://localhost:8080/user/user?page=2&size=5"
        },
        "last": {
          "href": "http://localhost:8080/user/user?page=2&size=5"
        }
      },
      "page": {
        "size": 5,
        "totalElements": 6,
        "totalPages": 2,
        "number": 0
      }
    

提交回复
热议问题