Skip to content

Commit fdb9c66

Browse files
committed
Register MVC validator with Spring Data REST HandlerAdapter.
Fixes: #967. Original pull request: 2108.
1 parent 0bbc8c0 commit fdb9c66

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/config/RepositoryRestMvcConfiguration.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@
121121
import org.springframework.plugin.core.PluginRegistry;
122122
import org.springframework.util.ClassUtils;
123123
import org.springframework.util.StringValueResolver;
124+
import org.springframework.validation.Validator;
124125
import org.springframework.web.bind.support.ConfigurableWebBindingInitializer;
125126
import org.springframework.web.cors.CorsConfiguration;
126127
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
@@ -653,6 +654,7 @@ public UriListHttpMessageConverter uriListHttpMessageConverter() {
653654
*/
654655
@Bean
655656
public RequestMappingHandlerAdapter repositoryExporterHandlerAdapter(
657+
@Qualifier("mvcValidator") ObjectProvider<Validator> validator,
656658
@Qualifier("defaultMessageConverters") List<HttpMessageConverter<?>> defaultMessageConverters,
657659
AlpsJsonHttpMessageConverter alpsJsonHttpMessageConverter, SelfLinkProvider selfLinkProvider,
658660
PersistentEntityResourceHandlerMethodArgumentResolver persistentEntityArgumentResolver,
@@ -662,6 +664,7 @@ public RequestMappingHandlerAdapter repositoryExporterHandlerAdapter(
662664
// Forward conversion service to handler adapter
663665
ConfigurableWebBindingInitializer initializer = new ConfigurableWebBindingInitializer();
664666
initializer.setConversionService(defaultConversionService);
667+
initializer.setValidator(validator.getIfUnique());
665668

666669
RepositoryRestHandlerAdapter handlerAdapter = new RepositoryRestHandlerAdapter(defaultMethodArgumentResolvers(
667670
selfLinkProvider, persistentEntityArgumentResolver, repoRequestArgumentResolver));

spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/config/RepositoryRestMvConfigurationIntegrationTests.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import org.springframework.data.geo.Distance;
4545
import org.springframework.data.geo.Point;
4646
import org.springframework.data.rest.webmvc.RepositoryLinksResource;
47+
import org.springframework.data.rest.webmvc.RepositoryRestHandlerAdapter;
4748
import org.springframework.data.rest.webmvc.RestMediaTypes;
4849
import org.springframework.data.rest.webmvc.alps.AlpsJsonHttpMessageConverter;
4950
import org.springframework.data.rest.webmvc.json.PersistentEntityJackson2Module;
@@ -61,6 +62,9 @@
6162
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
6263
import org.springframework.test.util.ReflectionTestUtils;
6364
import org.springframework.util.MultiValueMap;
65+
import org.springframework.validation.Validator;
66+
import org.springframework.validation.beanvalidation.OptionalValidatorFactoryBean;
67+
import org.springframework.web.bind.support.ConfigurableWebBindingInitializer;
6468
import org.springframework.web.servlet.HandlerMapping;
6569
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
6670
import org.springframework.web.util.UriComponentsBuilder;
@@ -193,6 +197,18 @@ void hasConvertersForPointAndDistance() {
193197
assertThat(service.canConvert(Distance.class, String.class)).isTrue();
194198
}
195199

200+
@Test // DATAREST-593 / #967
201+
void assertValidatorSupportWorkingCorrectly() {
202+
203+
RepositoryRestHandlerAdapter repositoryRestHandlerAdapter = context.getBean("repositoryExporterHandlerAdapter",
204+
RepositoryRestHandlerAdapter.class);
205+
206+
ConfigurableWebBindingInitializer configurableWebBindingInitializer = (ConfigurableWebBindingInitializer) repositoryRestHandlerAdapter
207+
.getWebBindingInitializer();
208+
209+
assertThat(configurableWebBindingInitializer.getValidator()).isNotNull();
210+
}
211+
196212
@Test // DATAREST-1198
197213
void hasConvertersForNamAndLdapName() {
198214

@@ -313,6 +329,11 @@ public LinkCollector customizeLinkCollector(LinkCollector collector) {
313329
}
314330
};
315331
}
332+
333+
@Bean
334+
Validator mvcValidator() {
335+
return new OptionalValidatorFactoryBean();
336+
}
316337
}
317338

318339
@Configuration

0 commit comments

Comments
 (0)