Skip to content

Commit 3b7c7c2

Browse files
committed
Merge pull request #9339 from vpavic:gh-7775
* pr/9339: Polish "Add Spring Data Web configuration properties" Add Spring Data Web configuration properties
2 parents 267014f + 677d52f commit 3b7c7c2

File tree

4 files changed

+200
-16
lines changed

4 files changed

+200
-16
lines changed

spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/web/SpringDataWebAutoConfiguration.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,14 @@
2323
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
2424
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication.Type;
2525
import org.springframework.boot.autoconfigure.data.rest.RepositoryRestMvcAutoConfiguration;
26+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
27+
import org.springframework.context.annotation.Bean;
2628
import org.springframework.context.annotation.Configuration;
29+
import org.springframework.data.domain.PageRequest;
2730
import org.springframework.data.web.PageableHandlerMethodArgumentResolver;
2831
import org.springframework.data.web.config.EnableSpringDataWebSupport;
32+
import org.springframework.data.web.config.PageableHandlerMethodArgumentResolverCustomizer;
33+
import org.springframework.data.web.config.SortHandlerMethodArgumentResolverCustomizer;
2934
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
3035

3136
/**
@@ -35,6 +40,7 @@
3540
* support through the {@link EnableSpringDataWebSupport} annotation.
3641
*
3742
* @author Andy Wilkinson
43+
* @author Vedran Pavic
3844
* @since 1.2.0
3945
*/
4046
@Configuration
@@ -43,7 +49,34 @@
4349
@ConditionalOnClass({ PageableHandlerMethodArgumentResolver.class,
4450
WebMvcConfigurer.class })
4551
@ConditionalOnMissingBean(PageableHandlerMethodArgumentResolver.class)
52+
@EnableConfigurationProperties(SpringDataWebProperties.class)
4653
@AutoConfigureAfter(RepositoryRestMvcAutoConfiguration.class)
4754
public class SpringDataWebAutoConfiguration {
4855

56+
private final SpringDataWebProperties properties;
57+
58+
public SpringDataWebAutoConfiguration(SpringDataWebProperties properties) {
59+
this.properties = properties;
60+
}
61+
62+
@Bean
63+
@ConditionalOnMissingBean
64+
public PageableHandlerMethodArgumentResolverCustomizer pageableCustomizer() {
65+
return pageableResolver -> {
66+
pageableResolver.setFallbackPageable(PageRequest.of(0,
67+
this.properties.getPageable().getDefaultPageSize()));
68+
pageableResolver.setPageParameterName(
69+
this.properties.getPageable().getPageParameter());
70+
pageableResolver.setSizeParameterName(
71+
this.properties.getPageable().getSizeParameter());
72+
};
73+
}
74+
75+
@Bean
76+
@ConditionalOnMissingBean
77+
public SortHandlerMethodArgumentResolverCustomizer sortCustomizer() {
78+
return sortResolver -> sortResolver
79+
.setSortParameter(this.properties.getSort().getSortParameter());
80+
}
81+
4982
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/*
2+
* Copyright 2012-2017 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.autoconfigure.data.web;
18+
19+
import org.springframework.boot.context.properties.ConfigurationProperties;
20+
21+
/**
22+
* Configuration properties for Spring Data Web.
23+
*
24+
* @author Vedran Pavic
25+
* @since 2.0.0
26+
*/
27+
@ConfigurationProperties("spring.data.web")
28+
public class SpringDataWebProperties {
29+
30+
private final Pageable pageable = new Pageable();
31+
32+
private final Sort sort = new Sort();
33+
34+
public Pageable getPageable() {
35+
return this.pageable;
36+
}
37+
38+
public Sort getSort() {
39+
return this.sort;
40+
}
41+
42+
/**
43+
* Pageable properties.
44+
*/
45+
public static class Pageable {
46+
47+
/**
48+
* Page index parameter name.
49+
*/
50+
private String pageParameter = "page";
51+
52+
/**
53+
* Page size parameter name.
54+
*/
55+
private String sizeParameter = "size";
56+
57+
/**
58+
* Default page size.
59+
*/
60+
private int defaultPageSize = 20;
61+
62+
public String getPageParameter() {
63+
return this.pageParameter;
64+
}
65+
66+
public void setPageParameter(String pageParameter) {
67+
this.pageParameter = pageParameter;
68+
}
69+
70+
public String getSizeParameter() {
71+
return this.sizeParameter;
72+
}
73+
74+
public void setSizeParameter(String sizeParameter) {
75+
this.sizeParameter = sizeParameter;
76+
}
77+
78+
public int getDefaultPageSize() {
79+
return this.defaultPageSize;
80+
}
81+
82+
public void setDefaultPageSize(int defaultPageSize) {
83+
this.defaultPageSize = defaultPageSize;
84+
}
85+
}
86+
87+
/**
88+
* Sort properties.
89+
*/
90+
public static class Sort {
91+
92+
/**
93+
* Sort parameter name.
94+
*/
95+
private String sortParameter = "sort";
96+
97+
public String getSortParameter() {
98+
return this.sortParameter;
99+
}
100+
101+
public void setSortParameter(String sortParameter) {
102+
this.sortParameter = sortParameter;
103+
}
104+
105+
}
106+
107+
}

spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/web/SpringDataWebAutoConfigurationTests.java

Lines changed: 54 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2016 the original author or authors.
2+
* Copyright 2012-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -21,10 +21,13 @@
2121
import org.junit.After;
2222
import org.junit.Test;
2323

24-
import org.springframework.context.ConfigurableApplicationContext;
24+
import org.springframework.boot.test.util.TestPropertyValues;
2525
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
26+
import org.springframework.data.domain.PageRequest;
2627
import org.springframework.data.web.PageableHandlerMethodArgumentResolver;
28+
import org.springframework.data.web.SortHandlerMethodArgumentResolver;
2729
import org.springframework.mock.web.MockServletContext;
30+
import org.springframework.test.util.ReflectionTestUtils;
2831
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
2932

3033
import static org.assertj.core.api.Assertions.assertThat;
@@ -33,10 +36,12 @@
3336
* Tests for {@link SpringDataWebAutoConfiguration}.
3437
*
3538
* @author Andy Wilkinson
39+
* @author Vedran Pavic
40+
* @author Stephane Nicoll
3641
*/
3742
public class SpringDataWebAutoConfigurationTests {
3843

39-
private ConfigurableApplicationContext context;
44+
private AnnotationConfigWebApplicationContext context;
4045

4146
@After
4247
public void after() {
@@ -47,26 +52,59 @@ public void after() {
4752

4853
@Test
4954
public void webSupportIsAutoConfiguredInWebApplicationContexts() {
50-
this.context = new AnnotationConfigWebApplicationContext();
51-
((AnnotationConfigWebApplicationContext) this.context)
52-
.register(SpringDataWebAutoConfiguration.class);
53-
this.context.refresh();
54-
((AnnotationConfigWebApplicationContext) this.context)
55-
.setServletContext(new MockServletContext());
55+
load();
56+
this.context.setServletContext(new MockServletContext());
5657
Map<String, PageableHandlerMethodArgumentResolver> beans = this.context
5758
.getBeansOfType(PageableHandlerMethodArgumentResolver.class);
5859
assertThat(beans).hasSize(1);
5960
}
6061

6162
@Test
6263
public void autoConfigurationBacksOffInNonWebApplicationContexts() {
63-
this.context = new AnnotationConfigApplicationContext();
64-
((AnnotationConfigApplicationContext) this.context)
65-
.register(SpringDataWebAutoConfiguration.class);
66-
this.context.refresh();
67-
Map<String, PageableHandlerMethodArgumentResolver> beans = this.context
68-
.getBeansOfType(PageableHandlerMethodArgumentResolver.class);
69-
assertThat(beans).isEmpty();
64+
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
65+
ctx.register(SpringDataWebAutoConfiguration.class);
66+
try {
67+
ctx.refresh();
68+
Map<String, PageableHandlerMethodArgumentResolver> beans = ctx
69+
.getBeansOfType(PageableHandlerMethodArgumentResolver.class);
70+
assertThat(beans).isEmpty();
71+
}
72+
finally {
73+
ctx.close();
74+
}
75+
}
76+
77+
@Test
78+
public void customizePageable() {
79+
load("spring.data.web.pageable.page-parameter=p",
80+
"spring.data.web.pageable.size-parameter=s",
81+
"spring.data.web.pageable.default-page-size=10");
82+
PageableHandlerMethodArgumentResolver argumentResolver = this.context
83+
.getBean(PageableHandlerMethodArgumentResolver.class);
84+
assertThat(ReflectionTestUtils.getField(argumentResolver, "pageParameterName"))
85+
.isEqualTo("p");
86+
assertThat(ReflectionTestUtils.getField(argumentResolver, "sizeParameterName"))
87+
.isEqualTo("s");
88+
assertThat(ReflectionTestUtils.getField(argumentResolver, "fallbackPageable"))
89+
.isEqualTo(PageRequest.of(0, 10));
90+
}
91+
92+
@Test
93+
public void customizeSort() {
94+
load("spring.data.web.sort.sort-parameter=s");
95+
SortHandlerMethodArgumentResolver argumentResolver = this.context
96+
.getBean(SortHandlerMethodArgumentResolver.class);
97+
assertThat(ReflectionTestUtils.getField(argumentResolver, "sortParameter"))
98+
.isEqualTo("s");
99+
}
100+
101+
private void load(String... environment) {
102+
AnnotationConfigWebApplicationContext ctx =
103+
new AnnotationConfigWebApplicationContext();
104+
TestPropertyValues.of(environment).applyTo(ctx);
105+
ctx.register(SpringDataWebAutoConfiguration.class);
106+
ctx.refresh();
107+
this.context = ctx;
70108
}
71109

72110
}

spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -652,6 +652,12 @@ content into your application; rather pick only the properties that you need.
652652
spring.data.solr.repositories.enabled=true # Enable Solr repositories.
653653
spring.data.solr.zk-host= # ZooKeeper host address in the form HOST:PORT.
654654
655+
# DATA WEB ({sc-spring-boot-autoconfigure}/data/web/SpringDataWebProperties.{sc-ext}[SpringDataWebProperties])
656+
spring.data.web.pageable.default-page-size=20 # Default page size.
657+
spring.data.web.pageable.page-parameter=page # Page index parameter name.
658+
spring.data.web.pageable.size-parameter=size # Page size parameter name.
659+
spring.data.web.sort.sort-parameter=sort # Sort parameter name.
660+
655661
# DATASOURCE ({sc-spring-boot-autoconfigure}/jdbc/DataSourceAutoConfiguration.{sc-ext}[DataSourceAutoConfiguration] & {sc-spring-boot-autoconfigure}/jdbc/DataSourceProperties.{sc-ext}[DataSourceProperties])
656662
spring.datasource.continue-on-error=false # Do not stop if an error occurs while initializing the database.
657663
spring.datasource.data= # Data (DML) script resource references.

0 commit comments

Comments
 (0)