spring-projects / spring-data-commons

Spring Data Commons. Interfaces and code shared between the various datastore specific implementations.
https://spring.io/projects/spring-data
Apache License 2.0
782 stars 675 forks source link

Respect oneIndexedParameters property when serialising Page to JSON #3161

Open clzola opened 2 months ago

clzola commented 2 months ago

I have noticed when using Spring Data in my project, I can set property spring.data.web.one-indexed-parameters to true and have my page numbering start from 1 in my API. However when returning Page from controller, this value is not respected and it will always return page number decreased by 1.

This PR fixes that by adding oneIndexedParameters attribute to SpringDataJacksonConfiguration.PageModule class. I have also added PageModuleCustomizer to be used to define wether PageModule will assume 1-based page number indexes in the request parameters

In order to respect this property, a PageModuleCustomizer needs to be defined:

@Bean 
public PageModuleCustomizer pageModuleCustomizer() {
    return pageModule -> {
        pageModule.setOneIndexedParameters(true); 
    }
}

Also this bean can be defined in Spring Boot Autoconfigure project, so spring will automatically take care of this.