Get spring boot pagination number starts from 1 in

2020-07-31 04:08发布

问题:

I am using spring boot for my API. There is a concept of pagination in my API so I am using spring-data-core 2.5.5 RELEASE for this. Now in response when I access the first page of my API the pagable object in the Response is as following

"page": { "size": 200, "totalElements": 15, "totalPages": 1, "number": 1 // always less than by 1 from the given page number }

I have already tried setting the property setOneIndexedParameters(true) but the issue still persists.

Please tell me a way to get it right without increasing the number in code.

回答1:

How did you set the property setOneIndexedParameters? To customize the pagination you just need to register a bean implementing the interface PageableHandlerMethodArgumentResolverCustomizer.

For example

@Configuration
public class CustomConfig {

    @Bean
    public PageableHandlerMethodArgumentResolverCustomizer customize() {
        return p -> p.setOneIndexedParameters(true);
    }
}

OR

@Component
public class CustomConfig implements 
                    PageableHandlerMethodArgumentResolverCustomizer {

    @Override
    public void customize(PageableHandlerMethodArgumentResolver pr) {
        pr.setOneIndexedParameters(true);
    }
}

Ref: HandlerMethodArgumentResolvers for Pageable and Sort