Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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.

like image 445
VelNaga Avatar asked Oct 05 '16 22:10

VelNaga


People also ask

How is spring boot implemented in pagination?

Once we have our repository extending from PagingAndSortingRepository, we just need to: Create or obtain a PageRequest object, which is an implementation of the Pageable interface. Pass the PageRequest object as an argument to the repository method we intend to use.

How can I paginate in JPA?

The simplest way to implement pagination is to use the Java Query Language – create a query and configure it via setMaxResults and setFirstResult: Query query = entityManager. createQuery("From Foo"); int pageNumber = 1; int pageSize = 10; query.

How do I set Pageable Sort?

Use PageableDefault annotation on the Pageable parameter to set default values for page number and page size. Use SortDefault to set default sort along with direction; and then you can also set multiple default sorts.


1 Answers

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.

like image 104
Przemek Nowak Avatar answered Sep 27 '22 16:09

Przemek Nowak