Skip to content

Commit ece8108

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

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;
@@ -650,6 +651,7 @@ public UriListHttpMessageConverter uriListHttpMessageConverter() {
650651
*/
651652
@Bean
652653
public RequestMappingHandlerAdapter repositoryExporterHandlerAdapter(
654+
@Qualifier("mvcValidator") ObjectProvider<Validator> validator,
653655
@Qualifier("defaultMessageConverters") List<HttpMessageConverter<?>> defaultMessageConverters,
654656
AlpsJsonHttpMessageConverter alpsJsonHttpMessageConverter, SelfLinkProvider selfLinkProvider,
655657
PersistentEntityResourceHandlerMethodArgumentResolver persistentEntityArgumentResolver,
@@ -659,6 +661,7 @@ public RequestMappingHandlerAdapter repositoryExporterHandlerAdapter(
659661
// Forward conversion service to handler adapter
660662
ConfigurableWebBindingInitializer initializer = new ConfigurableWebBindingInitializer();
661663
initializer.setConversionService(defaultConversionService);
664+
initializer.setValidator(validator.getIfUnique());
662665

663666
RepositoryRestHandlerAdapter handlerAdapter = new RepositoryRestHandlerAdapter(defaultMethodArgumentResolvers(
664667
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)