Merge pull request #9339 from vpavic:gh-7775
* pr/9339: Polish "Add Spring Data Web configuration properties" Add Spring Data Web configuration properties
This commit is contained in:
		
						commit
						3b7c7c2ddc
					
				| 
						 | 
				
			
			@ -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());
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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;
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -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,13 @@ import java.util.Map;
 | 
			
		|||
import org.junit.After;
 | 
			
		||||
import org.junit.Test;
 | 
			
		||||
 | 
			
		||||
import org.springframework.context.ConfigurableApplicationContext;
 | 
			
		||||
import org.springframework.boot.test.util.TestPropertyValues;
 | 
			
		||||
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,10 +36,12 @@ import static org.assertj.core.api.Assertions.assertThat;
 | 
			
		|||
 * Tests for {@link SpringDataWebAutoConfiguration}.
 | 
			
		||||
 *
 | 
			
		||||
 * @author Andy Wilkinson
 | 
			
		||||
 * @author Vedran Pavic
 | 
			
		||||
 * @author Stephane Nicoll
 | 
			
		||||
 */
 | 
			
		||||
public class SpringDataWebAutoConfigurationTests {
 | 
			
		||||
 | 
			
		||||
	private ConfigurableApplicationContext context;
 | 
			
		||||
	private AnnotationConfigWebApplicationContext context;
 | 
			
		||||
 | 
			
		||||
	@After
 | 
			
		||||
	public void after() {
 | 
			
		||||
| 
						 | 
				
			
			@ -47,12 +52,8 @@ public class SpringDataWebAutoConfigurationTests {
 | 
			
		|||
 | 
			
		||||
	@Test
 | 
			
		||||
	public void webSupportIsAutoConfiguredInWebApplicationContexts() {
 | 
			
		||||
		this.context = new AnnotationConfigWebApplicationContext();
 | 
			
		||||
		((AnnotationConfigWebApplicationContext) this.context)
 | 
			
		||||
				.register(SpringDataWebAutoConfiguration.class);
 | 
			
		||||
		this.context.refresh();
 | 
			
		||||
		((AnnotationConfigWebApplicationContext) this.context)
 | 
			
		||||
				.setServletContext(new MockServletContext());
 | 
			
		||||
		load();
 | 
			
		||||
		this.context.setServletContext(new MockServletContext());
 | 
			
		||||
		Map<String, PageableHandlerMethodArgumentResolver> beans = this.context
 | 
			
		||||
				.getBeansOfType(PageableHandlerMethodArgumentResolver.class);
 | 
			
		||||
		assertThat(beans).hasSize(1);
 | 
			
		||||
| 
						 | 
				
			
			@ -60,13 +61,50 @@ public class SpringDataWebAutoConfigurationTests {
 | 
			
		|||
 | 
			
		||||
	@Test
 | 
			
		||||
	public void autoConfigurationBacksOffInNonWebApplicationContexts() {
 | 
			
		||||
		this.context = new AnnotationConfigApplicationContext();
 | 
			
		||||
		((AnnotationConfigApplicationContext) this.context)
 | 
			
		||||
				.register(SpringDataWebAutoConfiguration.class);
 | 
			
		||||
		this.context.refresh();
 | 
			
		||||
		Map<String, PageableHandlerMethodArgumentResolver> beans = this.context
 | 
			
		||||
				.getBeansOfType(PageableHandlerMethodArgumentResolver.class);
 | 
			
		||||
		assertThat(beans).isEmpty();
 | 
			
		||||
		AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
 | 
			
		||||
		ctx.register(SpringDataWebAutoConfiguration.class);
 | 
			
		||||
		try {
 | 
			
		||||
			ctx.refresh();
 | 
			
		||||
			Map<String, PageableHandlerMethodArgumentResolver> beans = ctx
 | 
			
		||||
					.getBeansOfType(PageableHandlerMethodArgumentResolver.class);
 | 
			
		||||
			assertThat(beans).isEmpty();
 | 
			
		||||
		}
 | 
			
		||||
		finally {
 | 
			
		||||
			ctx.close();
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@Test
 | 
			
		||||
	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");
 | 
			
		||||
		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 customizeSort() {
 | 
			
		||||
		load("spring.data.web.sort.sort-parameter=s");
 | 
			
		||||
		SortHandlerMethodArgumentResolver argumentResolver = this.context
 | 
			
		||||
				.getBean(SortHandlerMethodArgumentResolver.class);
 | 
			
		||||
		assertThat(ReflectionTestUtils.getField(argumentResolver, "sortParameter"))
 | 
			
		||||
				.isEqualTo("s");
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	private void load(String... environment) {
 | 
			
		||||
		AnnotationConfigWebApplicationContext ctx =
 | 
			
		||||
				new AnnotationConfigWebApplicationContext();
 | 
			
		||||
		TestPropertyValues.of(environment).applyTo(ctx);
 | 
			
		||||
		ctx.register(SpringDataWebAutoConfiguration.class);
 | 
			
		||||
		ctx.refresh();
 | 
			
		||||
		this.context = ctx;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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.
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue