From be93eabc6b583713238655c2d7d2fcec782ef248 Mon Sep 17 00:00:00 2001 From: Marcel Overdijk Date: Fri, 22 Dec 2017 19:02:00 +0100 Subject: [PATCH 1/2] Improve Spring Data Web configuration properties See gh-11403 --- .../web/SpringDataWebAutoConfiguration.java | 4 ++ .../data/web/SpringDataWebProperties.java | 54 +++++++++++++++++++ .../SpringDataWebAutoConfigurationTests.java | 14 ++++- .../appendix-application-properties.adoc | 4 ++ 4 files changed, 75 insertions(+), 1 deletion(-) 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..38be5c4b501 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 @@ -69,6 +69,10 @@ public class SpringDataWebAutoConfiguration { PageRequest.of(0, pageable.getDefaultPageSize())); resolver.setPageParameterName(pageable.getPageParameter()); resolver.setSizeParameterName(pageable.getSizeParameter()); + resolver.setPrefix(pageable.getPrefix()); + resolver.setQualifierDelimiter(pageable.getQualifierDelimiter()); + resolver.setMaxPageSize(pageable.getMaxPageSize()); + resolver.setOneIndexedParameters(pageable.isOneIndexedParameters()); }; } 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..95e9961e1d3 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 @@ -59,6 +59,28 @@ public class SpringDataWebProperties { */ private int defaultPageSize = 20; + /** + * Configures a general prefix to be prepended to the page number and page size parameters. + */ + private String prefix; + + /** + * Configures the delimiter to be used between the qualifier and the actual page number and size properties. + */ + private String qualifierDelimiter; + + /** + * Configures the maximum page size to be accepted. + */ + private int maxPageSize = 2000; + + /** + * Whether to expose and assume 1-based page number indexes in the request parameters. + * Defaults to {@literal false}, meaning a page number of 0 in the request equals the first page. + * If this is set to {@literal true}, a page number of 1 in the request will be considered the first page. + */ + private boolean oneIndexedParameters = false; + public String getPageParameter() { return this.pageParameter; } @@ -82,6 +104,38 @@ public class SpringDataWebProperties { public void setDefaultPageSize(int defaultPageSize) { this.defaultPageSize = defaultPageSize; } + + public String getPrefix() { + return prefix; + } + + public void setPrefix(final String prefix) { + this.prefix = prefix; + } + + public String getQualifierDelimiter() { + return qualifierDelimiter; + } + + public void setQualifierDelimiter(final String qualifierDelimiter) { + this.qualifierDelimiter = qualifierDelimiter; + } + + public int getMaxPageSize() { + return maxPageSize; + } + + public void setMaxPageSize(final int maxPageSize) { + this.maxPageSize = maxPageSize; + } + + public boolean isOneIndexedParameters() { + return this.oneIndexedParameters; + } + + public void setOneIndexedParameters(final boolean oneIndexedParameters) { + this.oneIndexedParameters = oneIndexedParameters; + } } /** 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..64f22b10fa4 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,7 +78,11 @@ 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")) @@ -87,6 +91,14 @@ public class SpringDataWebAutoConfigurationTests { .isEqualTo("s"); assertThat(ReflectionTestUtils.getField(argumentResolver, "fallbackPageable")) .isEqualTo(PageRequest.of(0, 10)); + assertThat(ReflectionTestUtils.getField(argumentResolver, "prefix")) + .isEqualTo("abc"); + assertThat(ReflectionTestUtils.getField(argumentResolver, "qualifierDelimiter")) + .isEqualTo("__"); + assertThat(ReflectionTestUtils.getField(argumentResolver, "maxPageSize")) + .isEqualTo(100); + assertThat(ReflectionTestUtils.getField(argumentResolver, "oneIndexedParameters")) + .isEqualTo(true); } @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..142550af787 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 # Configures the maximum page size to be accepted. + spring.data.web.pageable.one-indexed-parameters=false # Whether to expose and assume 1-based page number indexes in the request parameters. spring.data.web.pageable.page-parameter=page # Page index parameter name. + spring.data.web.pageable.prefix= # Configures a general prefix to be prepended to the page number and page size parameters. + spring.data.web.pageable.qualifier-delimiter= # Configures the 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. From cfa3cab9881362deb36081b605f146bef01e9835 Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Thu, 28 Dec 2017 15:22:47 +0100 Subject: [PATCH 2/2] Polish "Improve Spring Data Web configuration properties" Closes gh-11403 --- .../web/SpringDataWebAutoConfiguration.java | 6 +- .../data/web/SpringDataWebProperties.java | 57 ++++++++++--------- .../SpringDataWebAutoConfigurationTests.java | 28 ++++++++- .../appendix-application-properties.adoc | 8 +-- 4 files changed, 61 insertions(+), 38 deletions(-) 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 38be5c4b501..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,14 +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()); - resolver.setOneIndexedParameters(pageable.isOneIndexedParameters()); }; } 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 95e9961e1d3..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 @@ -55,31 +55,31 @@ public class SpringDataWebProperties { private String sizeParameter = "size"; /** - * Default page 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 int defaultPageSize = 20; + private boolean oneIndexedParameters = false; /** - * Configures a general prefix to be prepended to the page number and page size parameters. + * General prefix to be prepended to the page number and page size parameters. */ - private String prefix; + private String prefix = ""; /** - * Configures the delimiter to be used between the qualifier and the actual page number and size properties. + * Delimiter to be used between the qualifier and the actual page number and size + * properties. */ - private String qualifierDelimiter; + private String qualifierDelimiter = "_"; /** - * Configures the maximum page size to be accepted. + * Default page size. */ - private int maxPageSize = 2000; + private int defaultPageSize = 20; /** - * Whether to expose and assume 1-based page number indexes in the request parameters. - * Defaults to {@literal false}, meaning a page number of 0 in the request equals the first page. - * If this is set to {@literal true}, a page number of 1 in the request will be considered the first page. + * Maximum page size to be accepted. */ - private boolean oneIndexedParameters = false; + private int maxPageSize = 2000; public String getPageParameter() { return this.pageParameter; @@ -97,45 +97,46 @@ public class SpringDataWebProperties { this.sizeParameter = sizeParameter; } - public int getDefaultPageSize() { - return this.defaultPageSize; + public boolean isOneIndexedParameters() { + return this.oneIndexedParameters; } - public void setDefaultPageSize(int defaultPageSize) { - this.defaultPageSize = defaultPageSize; + public void setOneIndexedParameters(boolean oneIndexedParameters) { + this.oneIndexedParameters = oneIndexedParameters; } public String getPrefix() { - return prefix; + return this.prefix; } - public void setPrefix(final String prefix) { + public void setPrefix(String prefix) { this.prefix = prefix; } public String getQualifierDelimiter() { - return qualifierDelimiter; + return this.qualifierDelimiter; } - public void setQualifierDelimiter(final String qualifierDelimiter) { + public void setQualifierDelimiter(String qualifierDelimiter) { this.qualifierDelimiter = qualifierDelimiter; } - public int getMaxPageSize() { - return maxPageSize; + public int getDefaultPageSize() { + return this.defaultPageSize; } - public void setMaxPageSize(final int maxPageSize) { - this.maxPageSize = maxPageSize; + public void setDefaultPageSize(int defaultPageSize) { + this.defaultPageSize = defaultPageSize; } - public boolean isOneIndexedParameters() { - return this.oneIndexedParameters; + public int getMaxPageSize() { + return this.maxPageSize; } - public void setOneIndexedParameters(final boolean oneIndexedParameters) { - this.oneIndexedParameters = oneIndexedParameters; + 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 64f22b10fa4..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 @@ -89,16 +89,38 @@ public class SpringDataWebAutoConfigurationTests { .isEqualTo("p"); assertThat(ReflectionTestUtils.getField(argumentResolver, "sizeParameterName")) .isEqualTo("s"); - assertThat(ReflectionTestUtils.getField(argumentResolver, "fallbackPageable")) - .isEqualTo(PageRequest.of(0, 10)); + 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(true); + .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 142550af787..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,11 +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 # Configures the maximum page size to be accepted. - spring.data.web.pageable.one-indexed-parameters=false # Whether to expose and assume 1-based page number indexes in the request parameters. + 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= # Configures a general prefix to be prepended to the page number and page size parameters. - spring.data.web.pageable.qualifier-delimiter= # Configures the delimiter to be used between the qualifier and the actual page number and size properties. + 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.