spring-data-commons icon indicating copy to clipboard operation
spring-data-commons copied to clipboard

Pagination from page 1 (one-indexed-parameters) not runs well

Open efimatica opened this issue 2 years ago • 5 comments

Good morning from spain. My scope is Spring boot 3.1 I'm implemeting REST service and I have pagination for return large list of elements. For default Spring boot manage the page from 0 index. I have reading that is possible start pagination from page 1, using this senetence in application.yaml:

spring:
  data:
    web:
      pageable:
        one-indexed-parameters: true

With new settings, the page 0 and the page 1 is the same. It show the same result. The problem is that the param "number" on paginations settings always show one page less, that I access:

Examples:

To page 0

Request: 
{{base_url}}/system/business/pagination?size=5&page=0

Response details:
    "pagination": {
    "totalPages": 4,
    "totalElements": 17,
    "size": 5,
    "number": 0,
    "numberOfElements": 5,
    "empty": false,
    "first": true,
    "last": false,
    "sorted": false
}

Note: I would prefer that it returns null. 

To page 1

Request: 
{{base_url}}/system/business/pagination?size=5&page=1

Response details:
    "pagination": {
    "totalPages": 4,
    "totalElements": 17,
    "size": 5,
    "number": 0,
    "numberOfElements": 5,
    "empty": false,
    "first": true,
    "last": false,
    "sorted": false
}

Note: Returns the same that page 0. See that number value is wrong. It would have to be 1. 

To page 2

Request: 
{{base_url}}/system/business/pagination?size=5&page=2

Response details:
    "pagination": {
    "totalPages": 4,
    "totalElements": 17,
    "size": 5,
    "number": 1,
    "numberOfElements": 5,
    "empty": false,
    "first": false,
    "last": false,
    "sorted": false
}

Note: Return the page 2, but number value is 1. 

Thanks for your attention

efimatica avatar Aug 01 '23 11:08 efimatica

I suspect you serialize a Page instance? That is not supported (as discussed in #1683) in the sense that we do not guarantee the serialization format. What you essentially see is the internal structure of the Page implementation, un-adapted to the parameter resolution style you have configured. Jackson simply doesn't know about that customization.

To produce proper and stable JSON representations use PagedResourcesAssembler (via DI) and call toModel(Page).

odrotbohm avatar Aug 01 '23 16:08 odrotbohm

Good morning,

Yes, I serialize the page instance, but I don't know if the problem lies there. When I use "one-indexed-parameters = true," the "number" value continues to use "zero-indexed-parameters." However, when "one-indexed-parameters = false," everything works well.

Regards,

efimatica avatar Aug 02 '23 08:08 efimatica

Same problem

velykov avatar Jul 12 '24 06:07 velykov

@odrotbohm

spring.data.web.pageable.serialization-mode=VIA_DTO changes nothing. The number field is always zero-indexed.

elijah-pl avatar Jan 10 '25 22:01 elijah-pl

I suspect you serialize a Page instance? That is not supported (as discussed in #1683) in the sense that we do not guarantee the serialization format. What you essentially see is the internal structure of the Page implementation, un-adapted to the parameter resolution style you have configured. Jackson simply doesn't know about that customization.

To produce proper and stable JSON representations use PagedResourcesAssembler (via DI) and call toModel(Page).

I don't think the issue is about serializing here, but more in having a symetrical request and response, e.g. if we use one-index for pageable, having the page number in the page aligned to the pageable would be desirable.

itsmoonrack avatar Aug 30 '25 23:08 itsmoonrack