diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/web/SpringDataWebAutoConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/web/SpringDataWebAutoConfiguration.java index 71dfc6a80da..256392b00a3 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/web/SpringDataWebAutoConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/web/SpringDataWebAutoConfiguration.java @@ -65,10 +65,14 @@ public class SpringDataWebAutoConfiguration { public PageableHandlerMethodArgumentResolverCustomizer pageableCustomizer() { return (resolver) -> { Pageable pageable = this.properties.getPageable(); - resolver.setFallbackPageable( - PageRequest.of(0, pageable.getDefaultPageSize())); resolver.setPageParameterName(pageable.getPageParameter()); resolver.setSizeParameterName(pageable.getSizeParameter()); + resolver.setOneIndexedParameters(pageable.isOneIndexedParameters()); + resolver.setPrefix(pageable.getPrefix()); + resolver.setQualifierDelimiter(pageable.getQualifierDelimiter()); + resolver.setFallbackPageable( + PageRequest.of(0, pageable.getDefaultPageSize())); + resolver.setMaxPageSize(pageable.getMaxPageSize()); }; } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/web/SpringDataWebProperties.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/web/SpringDataWebProperties.java index b56b03058d7..b32da512f65 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/web/SpringDataWebProperties.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/web/SpringDataWebProperties.java @@ -54,11 +54,33 @@ public class SpringDataWebProperties { */ private String sizeParameter = "size"; + /** + * Whether to expose and assume 1-based page number indexes. Defaults to "false", + * meaning a page number of 0 in the request equals the first page. + */ + private boolean oneIndexedParameters = false; + + /** + * General prefix to be prepended to the page number and page size parameters. + */ + private String prefix = ""; + + /** + * Delimiter to be used between the qualifier and the actual page number and size + * properties. + */ + private String qualifierDelimiter = "_"; + /** * Default page size. */ private int defaultPageSize = 20; + /** + * Maximum page size to be accepted. + */ + private int maxPageSize = 2000; + public String getPageParameter() { return this.pageParameter; } @@ -75,6 +97,30 @@ public class SpringDataWebProperties { this.sizeParameter = sizeParameter; } + public boolean isOneIndexedParameters() { + return this.oneIndexedParameters; + } + + public void setOneIndexedParameters(boolean oneIndexedParameters) { + this.oneIndexedParameters = oneIndexedParameters; + } + + public String getPrefix() { + return this.prefix; + } + + public void setPrefix(String prefix) { + this.prefix = prefix; + } + + public String getQualifierDelimiter() { + return this.qualifierDelimiter; + } + + public void setQualifierDelimiter(String qualifierDelimiter) { + this.qualifierDelimiter = qualifierDelimiter; + } + public int getDefaultPageSize() { return this.defaultPageSize; } @@ -82,6 +128,15 @@ public class SpringDataWebProperties { public void setDefaultPageSize(int defaultPageSize) { this.defaultPageSize = defaultPageSize; } + + public int getMaxPageSize() { + return this.maxPageSize; + } + + public void setMaxPageSize(int maxPageSize) { + this.maxPageSize = maxPageSize; + } + } /** diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/web/SpringDataWebAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/web/SpringDataWebAutoConfigurationTests.java index 2abc906fe8a..c2c8cd6d248 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/web/SpringDataWebAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/web/SpringDataWebAutoConfigurationTests.java @@ -78,15 +78,49 @@ public class SpringDataWebAutoConfigurationTests { public void customizePageable() { load("spring.data.web.pageable.page-parameter=p", "spring.data.web.pageable.size-parameter=s", - "spring.data.web.pageable.default-page-size=10"); + "spring.data.web.pageable.default-page-size=10", + "spring.data.web.pageable.prefix=abc", + "spring.data.web.pageable.qualifier-delimiter=__", + "spring.data.web.pageable.max-page-size=100", + "spring.data.web.pageable.one-indexed-parameters=true"); PageableHandlerMethodArgumentResolver argumentResolver = this.context .getBean(PageableHandlerMethodArgumentResolver.class); assertThat(ReflectionTestUtils.getField(argumentResolver, "pageParameterName")) .isEqualTo("p"); assertThat(ReflectionTestUtils.getField(argumentResolver, "sizeParameterName")) .isEqualTo("s"); + assertThat(ReflectionTestUtils.getField(argumentResolver, "oneIndexedParameters")) + .isEqualTo(true); + assertThat(ReflectionTestUtils.getField(argumentResolver, "prefix")) + .isEqualTo("abc"); + assertThat(ReflectionTestUtils.getField(argumentResolver, "qualifierDelimiter")) + .isEqualTo("__"); assertThat(ReflectionTestUtils.getField(argumentResolver, "fallbackPageable")) .isEqualTo(PageRequest.of(0, 10)); + assertThat(ReflectionTestUtils.getField(argumentResolver, "maxPageSize")) + .isEqualTo(100); + } + + @Test + public void defaultPageable() { + load(); + PageableHandlerMethodArgumentResolver argumentResolver = this.context + .getBean(PageableHandlerMethodArgumentResolver.class); + SpringDataWebProperties.Pageable properties = new SpringDataWebProperties().getPageable(); + assertThat(ReflectionTestUtils.getField(argumentResolver, "pageParameterName")) + .isEqualTo(properties.getPageParameter()); + assertThat(ReflectionTestUtils.getField(argumentResolver, "sizeParameterName")) + .isEqualTo(properties.getSizeParameter()); + assertThat(ReflectionTestUtils.getField(argumentResolver, "oneIndexedParameters")) + .isEqualTo(properties.isOneIndexedParameters()); + assertThat(ReflectionTestUtils.getField(argumentResolver, "prefix")) + .isEqualTo(properties.getPrefix()); + assertThat(ReflectionTestUtils.getField(argumentResolver, "qualifierDelimiter")) + .isEqualTo(properties.getQualifierDelimiter()); + assertThat(ReflectionTestUtils.getField(argumentResolver, "fallbackPageable")) + .isEqualTo(PageRequest.of(0, properties.getDefaultPageSize())); + assertThat(ReflectionTestUtils.getField(argumentResolver, "maxPageSize")) + .isEqualTo(properties.getMaxPageSize()); } @Test diff --git a/spring-boot-project/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc b/spring-boot-project/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc index 529ea993c0c..366ee5cf521 100644 --- a/spring-boot-project/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc +++ b/spring-boot-project/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc @@ -646,7 +646,11 @@ content into your application. Rather, pick only the properties that you need. # DATA WEB ({sc-spring-boot-autoconfigure}/data/web/SpringDataWebProperties.{sc-ext}[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.