Allow custom @Validated annotations for handler method parameters
Issue: SPR-12406
This commit is contained in:
@@ -31,6 +31,7 @@ import org.springframework.http.converter.HttpMessageConverter;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.validation.BindingResult;
|
||||
import org.springframework.validation.Errors;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.MethodArgumentNotValidException;
|
||||
import org.springframework.web.bind.WebDataBinder;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
@@ -223,10 +224,12 @@ public class RequestPartMethodArgumentResolver extends AbstractMessageConverterM
|
||||
|
||||
private void validate(WebDataBinder binder, MethodParameter parameter) throws MethodArgumentNotValidException {
|
||||
Annotation[] annotations = parameter.getParameterAnnotations();
|
||||
for (Annotation annot : annotations) {
|
||||
if (annot.annotationType().getSimpleName().startsWith("Valid")) {
|
||||
Object hints = AnnotationUtils.getValue(annot);
|
||||
binder.validate(hints instanceof Object[] ? (Object[]) hints : new Object[] {hints});
|
||||
for (Annotation ann : annotations) {
|
||||
Validated validatedAnn = AnnotationUtils.getAnnotation(ann, Validated.class);
|
||||
if (validatedAnn != null || ann.annotationType().getSimpleName().startsWith("Valid")) {
|
||||
Object hints = (validatedAnn != null ? validatedAnn.value() : AnnotationUtils.getValue(ann));
|
||||
Object[] validationHints = (hints instanceof Object[] ? (Object[]) hints : new Object[] {hints});
|
||||
binder.validate(validationHints);
|
||||
BindingResult bindingResult = binder.getBindingResult();
|
||||
if (bindingResult.hasErrors()) {
|
||||
if (isBindingErrorFatal(parameter)) {
|
||||
|
||||
@@ -33,6 +33,7 @@ import org.springframework.http.converter.HttpMessageNotReadableException;
|
||||
import org.springframework.http.server.ServletServerHttpRequest;
|
||||
import org.springframework.validation.BindingResult;
|
||||
import org.springframework.validation.Errors;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.HttpMediaTypeNotAcceptableException;
|
||||
import org.springframework.web.HttpMediaTypeNotSupportedException;
|
||||
import org.springframework.web.accept.ContentNegotiationManager;
|
||||
@@ -114,9 +115,11 @@ public class RequestResponseBodyMethodProcessor extends AbstractMessageConverter
|
||||
private void validate(WebDataBinder binder, MethodParameter parameter) throws Exception {
|
||||
Annotation[] annotations = parameter.getParameterAnnotations();
|
||||
for (Annotation ann : annotations) {
|
||||
if (ann.annotationType().getSimpleName().startsWith("Valid")) {
|
||||
Object hints = AnnotationUtils.getValue(ann);
|
||||
binder.validate(hints instanceof Object[] ? (Object[]) hints : new Object[] {hints});
|
||||
Validated validatedAnn = AnnotationUtils.getAnnotation(ann, Validated.class);
|
||||
if (validatedAnn != null || ann.annotationType().getSimpleName().startsWith("Valid")) {
|
||||
Object hints = (validatedAnn != null ? validatedAnn.value() : AnnotationUtils.getValue(ann));
|
||||
Object[] validationHints = (hints instanceof Object[] ? (Object[]) hints : new Object[] {hints});
|
||||
binder.validate(validationHints);
|
||||
BindingResult bindingResult = binder.getBindingResult();
|
||||
if (bindingResult.hasErrors()) {
|
||||
if (isBindExceptionRequired(binder, parameter)) {
|
||||
|
||||
@@ -16,8 +16,10 @@
|
||||
|
||||
package org.springframework.web.servlet.config;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
@@ -150,6 +152,7 @@ public class MvcNamespaceTests {
|
||||
|
||||
private HandlerMethod handlerMethod;
|
||||
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
TestMockServletContext servletContext = new TestMockServletContext();
|
||||
@@ -187,7 +190,7 @@ public class MvcNamespaceTests {
|
||||
|
||||
List<HttpMessageConverter<?>> converters = adapter.getMessageConverters();
|
||||
assertTrue(converters.size() > 0);
|
||||
for(HttpMessageConverter<?> converter : converters) {
|
||||
for (HttpMessageConverter<?> converter : converters) {
|
||||
if (converter instanceof AbstractJackson2HttpMessageConverter) {
|
||||
ObjectMapper objectMapper = ((AbstractJackson2HttpMessageConverter)converter).getObjectMapper();
|
||||
assertFalse(objectMapper.getDeserializationConfig().isEnabled(MapperFeature.DEFAULT_VIEW_INCLUSION));
|
||||
@@ -262,9 +265,6 @@ public class MvcNamespaceTests {
|
||||
doTestCustomValidator("mvc-config-custom-validator-32.xml");
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
private void doTestCustomValidator(String xml) throws Exception {
|
||||
loadBeanDefinitions(xml, 13);
|
||||
|
||||
@@ -808,12 +808,11 @@ public class MvcNamespaceTests {
|
||||
assertEquals(TestPathHelper.class, viewController.getUrlPathHelper().getClass());
|
||||
assertEquals(TestPathMatcher.class, viewController.getPathMatcher().getClass());
|
||||
|
||||
for(SimpleUrlHandlerMapping handlerMapping : appContext.getBeansOfType(SimpleUrlHandlerMapping.class).values()) {
|
||||
for (SimpleUrlHandlerMapping handlerMapping : appContext.getBeansOfType(SimpleUrlHandlerMapping.class).values()) {
|
||||
assertNotNull(handlerMapping);
|
||||
assertEquals(TestPathHelper.class, handlerMapping.getUrlPathHelper().getClass());
|
||||
assertEquals(TestPathMatcher.class, handlerMapping.getPathMatcher().getClass());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -827,13 +826,25 @@ public class MvcNamespaceTests {
|
||||
}
|
||||
|
||||
|
||||
@DateTimeFormat(iso=ISO.DATE)
|
||||
@Target({ElementType.PARAMETER})
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface IsoDate {
|
||||
}
|
||||
|
||||
@Validated(MyGroup.class)
|
||||
@Target({ElementType.PARAMETER})
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface MyValid {
|
||||
}
|
||||
|
||||
@Controller
|
||||
public static class TestController {
|
||||
|
||||
private boolean recordedValidationError;
|
||||
|
||||
@RequestMapping
|
||||
public void testBind(@RequestParam @DateTimeFormat(iso=ISO.DATE) Date date, @Validated(MyGroup.class) TestBean bean, BindingResult result) {
|
||||
public void testBind(@RequestParam @IsoDate Date date, @MyValid TestBean bean, BindingResult result) {
|
||||
this.recordedValidationError = (result.getErrorCount() == 1);
|
||||
}
|
||||
}
|
||||
@@ -885,9 +896,11 @@ public class MvcNamespaceTests {
|
||||
}
|
||||
}
|
||||
|
||||
public static class TestCallableProcessingInterceptor extends CallableProcessingInterceptorAdapter { }
|
||||
public static class TestCallableProcessingInterceptor extends CallableProcessingInterceptorAdapter {
|
||||
}
|
||||
|
||||
public static class TestDeferredResultProcessingInterceptor extends DeferredResultProcessingInterceptorAdapter { }
|
||||
public static class TestDeferredResultProcessingInterceptor extends DeferredResultProcessingInterceptorAdapter {
|
||||
}
|
||||
|
||||
public static class TestPathMatcher implements PathMatcher {
|
||||
|
||||
@@ -927,9 +940,11 @@ public class MvcNamespaceTests {
|
||||
}
|
||||
}
|
||||
|
||||
public static class TestPathHelper extends UrlPathHelper { }
|
||||
public static class TestPathHelper extends UrlPathHelper {
|
||||
}
|
||||
|
||||
public static class TestCacheManager implements CacheManager {
|
||||
|
||||
@Override
|
||||
public Cache getCache(String name) {
|
||||
return new ConcurrentMapCache(name);
|
||||
|
||||
@@ -2388,7 +2388,12 @@ public class ServletAnnotationControllerTests {
|
||||
@Controller
|
||||
private static class MyTypedCommandProvidingFormController
|
||||
extends MyCommandProvidingFormController<Integer, TestBean, ITestBean> {
|
||||
}
|
||||
|
||||
@Validated(MyGroup.class)
|
||||
@Target({ElementType.PARAMETER})
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface MyValid {
|
||||
}
|
||||
|
||||
@Controller
|
||||
@@ -2409,7 +2414,7 @@ public class ServletAnnotationControllerTests {
|
||||
|
||||
@Override
|
||||
@RequestMapping("/myPath.do")
|
||||
public String myHandle(@ModelAttribute("myCommand") @Validated(MyGroup.class) TestBean tb, BindingResult errors, ModelMap model) {
|
||||
public String myHandle(@ModelAttribute("myCommand") @MyValid TestBean tb, BindingResult errors, ModelMap model) {
|
||||
if (!errors.hasFieldErrors("sex")) {
|
||||
throw new IllegalStateException("requiredFields not applied");
|
||||
}
|
||||
@@ -2712,11 +2717,10 @@ public class ServletAnnotationControllerTests {
|
||||
}
|
||||
}
|
||||
|
||||
@Controller
|
||||
@Target({ElementType.TYPE})
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Controller
|
||||
public @interface MyControllerAnnotation {
|
||||
|
||||
}
|
||||
|
||||
@MyControllerAnnotation
|
||||
|
||||
Reference in New Issue
Block a user