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

34,697

Solution 1

If you are using Spring Boot 2.X you could switch from WebMvcConfigurerAdapter to application properties. There is a set of properties to configure pageable:

# DATA WEB (SpringDataWebProperties)
spring.data.web.pageable.default-page-size=20 # Default page size.
spring.data.web.pageable.max-page-size=2000 # Maximum page size to be accepted.
spring.data.web.pageable.one-indexed-parameters=false # Whether to expose and assume 1-based page number indexes.
spring.data.web.pageable.page-parameter=page # Page index parameter name.
spring.data.web.pageable.prefix= # General prefix to be prepended to the page number and page size parameters.
spring.data.web.pageable.qualifier-delimiter=_ # Delimiter to be used between the qualifier and the actual page number and size properties.
spring.data.web.pageable.size-parameter=size # Page size parameter name.
spring.data.web.sort.sort-parameter=sort # Sort parameter name.

But please remember that even if you change the one-indexed-parameter the page response (PageImpl class) will return results with zero-based page number. It could be a little misleading.

Solution 2

Spring added this future as well. Just make oneIndexed parameter equals to true in your configuration file and pagination will start from page 1.By default its false and pagination starts from 0.

spring.data.web.pageable.one-indexed-parameters=true

Solution 3

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<HandlerMethodArgumentResolver> argumentResolvers) {
        PageableHandlerMethodArgumentResolver resolver = new PageableHandlerMethodArgumentResolver();
        resolver.setOneIndexedParameters(true);
        argumentResolvers.add(resolver);
        super.addArgumentResolvers(argumentResolvers);
    }
}

Solution 4

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
  }
Share:
34,697

Related videos on Youtube

VelNaga
Author by

VelNaga

I'm here as a lifelong learner , I thank all the members the StackExchange's community for the great help that comes to me and I try in my turn to give my little help as I can. ......I love to learn,help others etc etc

Updated on September 11, 2020

Comments

  • VelNaga
    VelNaga over 3 years

    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 is there any standard approach to increment value instead of manually increment the page number inside the code?

    public Page<Device> find(DeviceFindCommand deviceFindCommand, Pageable pageable){
    //page = 0 //Actual is 0, Expected increment by 1. 
    }
    

    Any help should be appreciable.

    After implementing Alan answers having the following issues,

    1) Still i am able to access zero page which returns the first page(I don't know this is issue or not but i want to get a better clarity).

    http://localhost:8180/api/v1/books/?page=3&size=2

    Response

    {
        "content": [
    {
      "id": "57da9eadbee83fb037a66029",
      .
      .
      .
    }{
    .
    .
    .
    }
    ],
        "last": false,
        "totalElements": 5,
        "totalPages": 3,
        "size": 2,
        "number": 2, //strange always getting 1 less than page number.
        "sort": null,
        "first": true,
        "numberOfElements": 2
    }
    

    2) "number": 2, in the response always getting one less than the page number.It should return the current page index.

    • Cèsar
      Cèsar over 7 years
      Which technology you use to develop the front-end pages?
    • VelNaga
      VelNaga over 7 years
      Spring-boot 1.4.0
    • VelNaga
      VelNaga over 7 years
      @Cèsar Front end using Ember 2.7 s
    • Andrés Pizá Bückmann
      Andrés Pizá Bückmann almost 7 years
      Did you ever solve the "0" in "number"? I'm seeing this as well.
    • VelNaga
      VelNaga almost 7 years
      @Tovkal you can check the Alan Hay answer it might help you...Other than that i couldn't find any other way...
  • VelNaga
    VelNaga over 7 years
    It is working but i am facing couple of issues.still i am able to access the zero indexed page URL "/books?page=0&size=2".Then "number" attribute in the response is still zero indexed, I am accessing page:3 but getting number attribute as 2, "last": true,"totalElements": 5, "totalPages": 3,"size": 2,"number": 2,"sort": null,"first": false, "numberOfElements": 1". Please guide me to resolve this issue.
  • Alan Hay
    Alan Hay over 7 years
    Sorry but I do not understand the issue: You want to use 1 based paging but call "/books?page=0&size=2"
  • VelNaga
    VelNaga over 7 years
    let me edit my question so that you will get a better clarity
  • VelNaga
    VelNaga over 7 years
    I have edited my post please guide me to resolve this
  • ahmedjaad
    ahmedjaad almost 6 years
    Did you find the solution for this?
  • Stephane
    Stephane over 5 years
    I submitted an issue regarding the response displaying a zero based current page even if configuring for a one based pagination jira.spring.io/browse/DATACMNS-563 I must admit I am still confused as to what is expected by the api here.
  • gary
    gary over 5 years
    I have the same problem in the response. To follow your example, if I request: /books?page=1 then I get a page object in the response, page: { ... number: 0 ... }. The page.number returns 0 when I asked for page 1. page.number = 1 when I ask for page 2 etc.
  • Sandeep Kumar
    Sandeep Kumar about 4 years
    It's better you add some details as well to support your answer as well.