Skip to content

Commit c1036b2

Browse files
vpavicodrotbohm
authored andcommitted
DATACMNS-822 - Provide customizers for default pageable and sort resolvers.
Introduced dedicated callback interfaces to customize the HandlerMethodArgumentResolver instances registered by SpringDataWebConfiguration. This allows bean definition registration of those customizer interfaces instead of having to extend a configuration class. Original pull request: #208.
1 parent 34ce83c commit c1036b2

File tree

5 files changed

+166
-5
lines changed

5 files changed

+166
-5
lines changed

src/main/java/org/springframework/data/web/config/HateoasAwareSpringDataWebConfiguration.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013 the original author or authors.
2+
* Copyright 2013-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.
@@ -36,6 +36,7 @@
3636
* @author Oliver Gierke
3737
* @author Nick Williams
3838
* @author Ben Hale
39+
* @author Vedran Pavic
3940
*/
4041
@Configuration
4142
public class HateoasAwareSpringDataWebConfiguration extends SpringDataWebConfiguration {
@@ -56,7 +57,10 @@ public HateoasAwareSpringDataWebConfiguration(ApplicationContext context,
5657
@Bean
5758
@Override
5859
public HateoasPageableHandlerMethodArgumentResolver pageableResolver() {
59-
return new HateoasPageableHandlerMethodArgumentResolver(sortResolver());
60+
HateoasPageableHandlerMethodArgumentResolver pageableResolver =
61+
new HateoasPageableHandlerMethodArgumentResolver(sortResolver());
62+
customizePageableResolver(pageableResolver);
63+
return pageableResolver;
6064
}
6165

6266
/*
@@ -66,7 +70,9 @@ public HateoasPageableHandlerMethodArgumentResolver pageableResolver() {
6670
@Bean
6771
@Override
6872
public HateoasSortHandlerMethodArgumentResolver sortResolver() {
69-
return new HateoasSortHandlerMethodArgumentResolver();
73+
HateoasSortHandlerMethodArgumentResolver sortResolver = new HateoasSortHandlerMethodArgumentResolver();
74+
customizeSortResolver(sortResolver);
75+
return sortResolver;
7076
}
7177

7278
@Bean
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright 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+
package org.springframework.data.web.config;
17+
18+
import org.springframework.data.web.PageableHandlerMethodArgumentResolver;
19+
20+
/**
21+
* Callback interface that can be implemented by beans wishing to customize the
22+
* {@link PageableHandlerMethodArgumentResolver} configuration.
23+
*
24+
* @author Vedran Pavic
25+
*/
26+
public interface PageableHandlerMethodArgumentResolverCustomizer {
27+
28+
/**
29+
* Customize the pageable resolver
30+
*
31+
* @param pageableResolver the {@link PageableHandlerMethodArgumentResolver} to customize
32+
*/
33+
void customize(PageableHandlerMethodArgumentResolver pageableResolver);
34+
35+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright 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+
package org.springframework.data.web.config;
17+
18+
import org.springframework.data.web.SortHandlerMethodArgumentResolver;
19+
20+
/**
21+
* Callback interface that can be implemented by beans wishing to customize the
22+
* {@link SortHandlerMethodArgumentResolver} configuration.
23+
*
24+
* @author Vedran Pavic
25+
*/
26+
public interface SortHandlerMethodArgumentResolverCustomizer {
27+
28+
/**
29+
* Customize the sort resolver
30+
*
31+
* @param sortResolver the {@link SortHandlerMethodArgumentResolver} to customize
32+
*/
33+
void customize(SortHandlerMethodArgumentResolver sortResolver);
34+
35+
}

src/main/java/org/springframework/data/web/config/SpringDataWebConfiguration.java

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
*
4747
* @since 1.6
4848
* @author Oliver Gierke
49+
* @author Vedran Pavic
4950
* @author Jens Schauder
5051
*/
5152
@Configuration
@@ -61,13 +62,22 @@ public SpringDataWebConfiguration(ApplicationContext context,
6162
this.conversionService = conversionService;
6263
}
6364

65+
@Autowired(required = false)
66+
private PageableHandlerMethodArgumentResolverCustomizer pageableResolverCustomizer;
67+
68+
@Autowired(required = false)
69+
private SortHandlerMethodArgumentResolverCustomizer sortResolverCustomizer;
70+
6471
/*
6572
* (non-Javadoc)
6673
* @see org.springframework.data.web.config.SpringDataWebConfiguration#pageableResolver()
6774
*/
6875
@Bean
6976
public PageableHandlerMethodArgumentResolver pageableResolver() {
70-
return new PageableHandlerMethodArgumentResolver(sortResolver());
77+
PageableHandlerMethodArgumentResolver pageableResolver =
78+
new PageableHandlerMethodArgumentResolver(sortResolver());
79+
customizePageableResolver(pageableResolver);
80+
return pageableResolver;
7181
}
7282

7383
/*
@@ -76,7 +86,9 @@ public PageableHandlerMethodArgumentResolver pageableResolver() {
7686
*/
7787
@Bean
7888
public SortHandlerMethodArgumentResolver sortResolver() {
79-
return new SortHandlerMethodArgumentResolver();
89+
SortHandlerMethodArgumentResolver sortResolver = new SortHandlerMethodArgumentResolver();
90+
customizeSortResolver(sortResolver);
91+
return sortResolver;
8092
}
8193

8294
/*
@@ -139,4 +151,17 @@ public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
139151
converters.add(0, new XmlBeamHttpMessageConverter());
140152
}
141153
}
154+
155+
protected void customizePageableResolver(PageableHandlerMethodArgumentResolver pageableResolver) {
156+
if (this.pageableResolverCustomizer != null) {
157+
this.pageableResolverCustomizer.customize(pageableResolver);
158+
}
159+
}
160+
161+
protected void customizeSortResolver(SortHandlerMethodArgumentResolver sortResolver) {
162+
if (this.sortResolverCustomizer != null) {
163+
this.sortResolverCustomizer.customize(sortResolver);
164+
}
165+
}
166+
142167
}

src/test/java/org/springframework/data/web/config/EnableSpringDataWebSupportIntegrationTests.java

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import org.springframework.data.web.SortHandlerMethodArgumentResolver;
3636
import org.springframework.data.web.WebTestUtils;
3737
import org.springframework.hateoas.Link;
38+
import org.springframework.test.util.ReflectionTestUtils;
3839
import org.springframework.test.web.servlet.MockMvc;
3940
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
4041
import org.springframework.web.context.WebApplicationContext;
@@ -50,6 +51,7 @@
5051
*
5152
* @author Oliver Gierke
5253
* @author Jens Schauder
54+
* @author Vedran Pavic
5355
*/
5456
public class EnableSpringDataWebSupportIntegrationTests {
5557

@@ -63,6 +65,44 @@ static class SampleConfig {
6365
}
6466
}
6567

68+
@Configuration
69+
@EnableWebMvc
70+
@EnableSpringDataWebSupport
71+
static class PageableResolverCustomizerConfig extends SampleConfig {
72+
73+
@Bean
74+
public PageableHandlerMethodArgumentResolverCustomizer testPageableResolverCustomizer() {
75+
return new PageableHandlerMethodArgumentResolverCustomizer() {
76+
77+
@Override
78+
public void customize(PageableHandlerMethodArgumentResolver pageableResolver) {
79+
pageableResolver.setMaxPageSize(100);
80+
}
81+
82+
};
83+
}
84+
85+
}
86+
87+
@Configuration
88+
@EnableWebMvc
89+
@EnableSpringDataWebSupport
90+
static class SortResolverCustomizerConfig extends SampleConfig {
91+
92+
@Bean
93+
public SortHandlerMethodArgumentResolverCustomizer testSortResolverCustomizer() {
94+
return new SortHandlerMethodArgumentResolverCustomizer() {
95+
96+
@Override
97+
public void customize(SortHandlerMethodArgumentResolver sortResolver) {
98+
sortResolver.setSortParameter("foo");
99+
}
100+
101+
};
102+
}
103+
104+
}
105+
66106
@Test // DATACMNS-330
67107
public void registersBasicBeanDefinitions() throws Exception {
68108

@@ -159,6 +199,26 @@ public void picksUpWebConfigurationMixins() {
159199
assertThat(names).contains("sampleBean");
160200
}
161201

202+
@Test // DATACMNS-822
203+
public void picksUpPageableResolverCustomizer() {
204+
ApplicationContext context = WebTestUtils.createApplicationContext(PageableResolverCustomizerConfig.class);
205+
List<String> names = Arrays.asList(context.getBeanDefinitionNames());
206+
PageableHandlerMethodArgumentResolver resolver = context.getBean(PageableHandlerMethodArgumentResolver.class);
207+
208+
assertThat(names, hasItem("testPageableResolverCustomizer"));
209+
assertThat((Integer) ReflectionTestUtils.getField(resolver, "maxPageSize"), equalTo(100));
210+
}
211+
212+
@Test // DATACMNS-822
213+
public void picksUpSortResolverCustomizer() {
214+
ApplicationContext context = WebTestUtils.createApplicationContext(SortResolverCustomizerConfig.class);
215+
List<String> names = Arrays.asList(context.getBeanDefinitionNames());
216+
SortHandlerMethodArgumentResolver resolver = context.getBean(SortHandlerMethodArgumentResolver.class);
217+
218+
assertThat(names, hasItem("testSortResolverCustomizer"));
219+
assertThat((String) ReflectionTestUtils.getField(resolver, "sortParameter"), equalTo("foo"));
220+
}
221+
162222
private static void assertResolversRegistered(ApplicationContext context, Class<?>... resolverTypes) {
163223

164224
RequestMappingHandlerAdapter adapter = context.getBean(RequestMappingHandlerAdapter.class);

0 commit comments

Comments
 (0)