From e9ac41f83f5783c4dfba1da88c1253bb34a87600 Mon Sep 17 00:00:00 2001 From: Vedran Pavic Date: Mon, 29 May 2017 00:37:39 +0200 Subject: [PATCH] Add Spring Data Web configuration properties This commit adds support for configuring Spring Data Web `PageableHandlerMethodArgumentResolver` and `SortHandlerMethodArgumentResolver` using configuration properties. See gh-9339 --- .../web/SpringDataWebAutoConfiguration.java | 33 ++++++ .../data/web/SpringDataWebProperties.java | 107 ++++++++++++++++++ .../SpringDataWebAutoConfigurationTests.java | 42 ++++++- .../appendix-application-properties.adoc | 6 + 4 files changed, 187 insertions(+), 1 deletion(-) create mode 100644 spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/web/SpringDataWebProperties.java diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/web/SpringDataWebAutoConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/web/SpringDataWebAutoConfiguration.java index 13d85d7feb..932e632b3f 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/web/SpringDataWebAutoConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/web/SpringDataWebAutoConfiguration.java @@ -23,9 +23,14 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication.Type; import org.springframework.boot.autoconfigure.data.rest.RepositoryRestMvcAutoConfiguration; +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.data.domain.PageRequest; import org.springframework.data.web.PageableHandlerMethodArgumentResolver; import org.springframework.data.web.config.EnableSpringDataWebSupport; +import org.springframework.data.web.config.PageableHandlerMethodArgumentResolverCustomizer; +import org.springframework.data.web.config.SortHandlerMethodArgumentResolverCustomizer; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; /** @@ -35,6 +40,7 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; * support through the {@link EnableSpringDataWebSupport} annotation. * * @author Andy Wilkinson + * @author Vedran Pavic * @since 1.2.0 */ @Configuration @@ -43,7 +49,34 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @ConditionalOnClass({ PageableHandlerMethodArgumentResolver.class, WebMvcConfigurer.class }) @ConditionalOnMissingBean(PageableHandlerMethodArgumentResolver.class) +@EnableConfigurationProperties(SpringDataWebProperties.class) @AutoConfigureAfter(RepositoryRestMvcAutoConfiguration.class) public class SpringDataWebAutoConfiguration { + private final SpringDataWebProperties properties; + + public SpringDataWebAutoConfiguration(SpringDataWebProperties properties) { + this.properties = properties; + } + + @Bean + @ConditionalOnMissingBean + public PageableHandlerMethodArgumentResolverCustomizer pageableCustomizer() { + return pageableResolver -> { + pageableResolver.setFallbackPageable(PageRequest.of(0, + this.properties.getPageable().getDefaultPageSize())); + pageableResolver.setPageParameterName( + this.properties.getPageable().getPageParameter()); + pageableResolver.setSizeParameterName( + this.properties.getPageable().getSizeParameter()); + }; + } + + @Bean + @ConditionalOnMissingBean + public SortHandlerMethodArgumentResolverCustomizer sortCustomizer() { + return sortResolver -> sortResolver + .setSortParameter(this.properties.getSort().getSortParameter()); + } + } diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/web/SpringDataWebProperties.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/web/SpringDataWebProperties.java new file mode 100644 index 0000000000..b56b03058d --- /dev/null +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/web/SpringDataWebProperties.java @@ -0,0 +1,107 @@ +/* + * Copyright 2012-2017 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.autoconfigure.data.web; + +import org.springframework.boot.context.properties.ConfigurationProperties; + +/** + * Configuration properties for Spring Data Web. + * + * @author Vedran Pavic + * @since 2.0.0 + */ +@ConfigurationProperties("spring.data.web") +public class SpringDataWebProperties { + + private final Pageable pageable = new Pageable(); + + private final Sort sort = new Sort(); + + public Pageable getPageable() { + return this.pageable; + } + + public Sort getSort() { + return this.sort; + } + + /** + * Pageable properties. + */ + public static class Pageable { + + /** + * Page index parameter name. + */ + private String pageParameter = "page"; + + /** + * Page size parameter name. + */ + private String sizeParameter = "size"; + + /** + * Default page size. + */ + private int defaultPageSize = 20; + + public String getPageParameter() { + return this.pageParameter; + } + + public void setPageParameter(String pageParameter) { + this.pageParameter = pageParameter; + } + + public String getSizeParameter() { + return this.sizeParameter; + } + + public void setSizeParameter(String sizeParameter) { + this.sizeParameter = sizeParameter; + } + + public int getDefaultPageSize() { + return this.defaultPageSize; + } + + public void setDefaultPageSize(int defaultPageSize) { + this.defaultPageSize = defaultPageSize; + } + } + + /** + * Sort properties. + */ + public static class Sort { + + /** + * Sort parameter name. + */ + private String sortParameter = "sort"; + + public String getSortParameter() { + return this.sortParameter; + } + + public void setSortParameter(String sortParameter) { + this.sortParameter = sortParameter; + } + + } + +} diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/web/SpringDataWebAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/web/SpringDataWebAutoConfigurationTests.java index 85c2faaf37..971c56180e 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/web/SpringDataWebAutoConfigurationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/web/SpringDataWebAutoConfigurationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2016 the original author or authors. + * Copyright 2012-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -21,10 +21,14 @@ import java.util.Map; import org.junit.After; import org.junit.Test; +import org.springframework.boot.test.util.TestPropertyValues; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; +import org.springframework.data.domain.PageRequest; import org.springframework.data.web.PageableHandlerMethodArgumentResolver; +import org.springframework.data.web.SortHandlerMethodArgumentResolver; import org.springframework.mock.web.MockServletContext; +import org.springframework.test.util.ReflectionTestUtils; import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; import static org.assertj.core.api.Assertions.assertThat; @@ -33,6 +37,7 @@ import static org.assertj.core.api.Assertions.assertThat; * Tests for {@link SpringDataWebAutoConfiguration}. * * @author Andy Wilkinson + * @author Vedran Pavic */ public class SpringDataWebAutoConfigurationTests { @@ -69,4 +74,39 @@ public class SpringDataWebAutoConfigurationTests { assertThat(beans).isEmpty(); } + @Test + public void pageable() { + this.context = new AnnotationConfigWebApplicationContext(); + TestPropertyValues + .of("spring.data.web.pageable.page-parameter=p", + "spring.data.web.pageable.size-parameter=s", + "spring.data.web.pageable.default-page-size=10") + .applyTo(this.context); + ((AnnotationConfigWebApplicationContext) this.context) + .register(SpringDataWebAutoConfiguration.class); + this.context.refresh(); + 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, "fallbackPageable")) + .isEqualTo(PageRequest.of(0, 10)); + } + + @Test + public void sort() { + this.context = new AnnotationConfigWebApplicationContext(); + TestPropertyValues.of("spring.data.web.sort.sort-parameter=s") + .applyTo(this.context); + ((AnnotationConfigWebApplicationContext) this.context) + .register(SpringDataWebAutoConfiguration.class); + this.context.refresh(); + SortHandlerMethodArgumentResolver argumentResolver = this.context + .getBean(SortHandlerMethodArgumentResolver.class); + assertThat(ReflectionTestUtils.getField(argumentResolver, "sortParameter")) + .isEqualTo("s"); + } + } diff --git a/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc b/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc index d8b95cc15d..be9b741429 100644 --- a/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc +++ b/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc @@ -652,6 +652,12 @@ content into your application; rather pick only the properties that you need. spring.data.solr.repositories.enabled=true # Enable Solr repositories. spring.data.solr.zk-host= # ZooKeeper host address in the form HOST:PORT. + # 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.page-parameter=page # Page index parameter name. + spring.data.web.pageable.size-parameter=size # Page size parameter name. + spring.data.web.sort.sort-parameter=sort # Sort parameter name. + # DATASOURCE ({sc-spring-boot-autoconfigure}/jdbc/DataSourceAutoConfiguration.{sc-ext}[DataSourceAutoConfiguration] & {sc-spring-boot-autoconfigure}/jdbc/DataSourceProperties.{sc-ext}[DataSourceProperties]) spring.datasource.continue-on-error=false # Do not stop if an error occurs while initializing the database. spring.datasource.data= # Data (DML) script resource references.