added SmartValidator interface with general support for validation hints; added custom @Valid annotation with support for JSR-303 validation groups; JSR-303 SpringValidatorAdapter and MVC data binding provide support for validation groups (SPR-6373)

This commit is contained in:
Juergen Hoeller 2011-12-03 15:44:33 +00:00
parent 4bfcb79ae3
commit 49a2aaf023
11 changed files with 324 additions and 142 deletions

View File

@ -706,8 +706,22 @@ public class DataBinder implements PropertyEditorRegistry, TypeConverter {
* @see #getBindingResult() * @see #getBindingResult()
*/ */
public void validate() { public void validate() {
this.validator.validate(getTarget(), getBindingResult());
}
/**
* Invoke the specified Validator, if any, with the given validation hints.
* <p>Note: Validation hints may get ignored by the actual target Validator.
* @param validationHints one or more hint objects to be passed to a {@link SmartValidator}
* @see #setValidator(Validator)
* @see SmartValidator#validate(Object, Errors, Object...)
*/
public void validate(Object... validationHints) {
Validator validator = getValidator(); Validator validator = getValidator();
if (validator != null) { if (validator instanceof SmartValidator) {
((SmartValidator) validator).validate(getTarget(), getBindingResult(), validationHints);
}
else if (validator != null) {
validator.validate(getTarget(), getBindingResult()); validator.validate(getTarget(), getBindingResult());
} }
} }

View File

@ -0,0 +1,47 @@
/*
* Copyright 2002-2011 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.validation;
/**
* Extended variant of the {@link Validator} interface, adding support for
* validation 'hints'.
*
* @author Juergen Hoeller
* @since 3.1
*/
public interface SmartValidator extends Validator {
/**
* Validate the supplied <code>target</code> object, which must be
* of a {@link Class} for which the {@link #supports(Class)} method
* typically has (or would) return <code>true</code>.
* <p>The supplied {@link Errors errors} instance can be used to report
* any resulting validation errors.
* <p><b>This variant of <code>validate</code> supports validation hints,
* such as validation groups against a JSR-303 provider</b> (in this case,
* the provided hint objects need to be annotation arguments of type Class).
* <p>Note: Validation hints may get ignored by the actual target Validator,
* in which case this method is supposed to be behave just like its regular
* {@link #validate(Object, Errors)} sibling.
* @param target the object that is to be validated (can be <code>null</code>)
* @param errors contextual state about the validation process (never <code>null</code>)
* @param validationHints one or more hint objects to be passed to the validation engine
* @see ValidationUtils
*/
void validate(Object target, Errors errors, Object... validationHints);
}

View File

@ -0,0 +1,56 @@
/*
* Copyright 2002-2011 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.validation.annotation;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* Extended variant of JSR-303's {@link javax.validation.Valid},
* supporting the specification of validation groups. Designed for
* convenient use with Spring's JSR-303 support but not JSR-303 specific.
*
* <p>Can be used e.g. with Spring MVC handler methods arguments.
* Supported through {@link org.springframework.validation.SmartValidator}'s
* validation hint concept, with validation group classes acting as hint objects.
*
* @author Juergen Hoeller
* @since 3.1
* @see javax.validation.Validator#validate(Object, Class[])
* @see org.springframework.validation.SmartValidator#validate(Object, org.springframework.validation.Errors, Object...)
* @see org.springframework.validation.beanvalidation.SpringValidatorAdapter
*/
@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Valid {
/**
* Specify one or more validation groups to apply to the validation step
* kicked off by this annotation.
* <p>JSR-303 defines validation groups as custom annotations which an application declares
* for the sole purpose of using them as type-safe group arguments, as implemented in
* {@link org.springframework.validation.beanvalidation.SpringValidatorAdapter}.
* <p>Other {@link org.springframework.validation.SmartValidator} implementations may
* support class arguments in other ways as well.
*/
Class[] value() default {};
}

View File

@ -0,0 +1,8 @@
/**
* Support classes for annotation-based constraint evaluation,
* e.g. using a JSR-303 Bean Validation provider.
*
* <p>Provides an extended variant of JSR-303's <code>@Valid</code>,
* supporting the specification of validation groups.
*/
package org.springframework.validation.annotation;

View File

@ -17,6 +17,7 @@
package org.springframework.validation.beanvalidation; package org.springframework.validation.beanvalidation;
import java.util.HashSet; import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -33,7 +34,7 @@ import org.springframework.validation.BindingResult;
import org.springframework.validation.Errors; import org.springframework.validation.Errors;
import org.springframework.validation.FieldError; import org.springframework.validation.FieldError;
import org.springframework.validation.ObjectError; import org.springframework.validation.ObjectError;
import org.springframework.validation.Validator; import org.springframework.validation.SmartValidator;
/** /**
* Adapter that takes a JSR-303 <code>javax.validator.Validator</code> * Adapter that takes a JSR-303 <code>javax.validator.Validator</code>
@ -46,7 +47,7 @@ import org.springframework.validation.Validator;
* @author Juergen Hoeller * @author Juergen Hoeller
* @since 3.0 * @since 3.0
*/ */
public class SpringValidatorAdapter implements Validator, javax.validation.Validator { public class SpringValidatorAdapter implements SmartValidator, javax.validation.Validator {
private static final Set<String> internalAnnotationAttributes = new HashSet<String>(3); private static final Set<String> internalAnnotationAttributes = new HashSet<String>(3);
@ -85,8 +86,29 @@ public class SpringValidatorAdapter implements Validator, javax.validation.Valid
} }
public void validate(Object target, Errors errors) { public void validate(Object target, Errors errors) {
Set<ConstraintViolation<Object>> result = this.targetValidator.validate(target); processConstraintViolations(this.targetValidator.validate(target), errors);
for (ConstraintViolation<Object> violation : result) { }
public void validate(Object target, Errors errors, Object[] validationHints) {
Set<Class> groups = new LinkedHashSet<Class>();
if (validationHints != null) {
for (Object hint : validationHints) {
if (hint instanceof Class) {
groups.add((Class) hint);
}
}
}
processConstraintViolations(this.targetValidator.validate(target, groups.toArray(new Class[groups.size()])), errors);
}
/**
* Process the given JSR-303 ConstraintViolations, adding corresponding errors to
* the provided Spring {@link Errors} object.
* @param violations the JSR-303 ConstraintViolation results
* @param errors the Spring errors object to register to
*/
protected void processConstraintViolations(Set<ConstraintViolation<Object>> violations, Errors errors) {
for (ConstraintViolation<Object> violation : violations) {
String field = violation.getPropertyPath().toString(); String field = violation.getPropertyPath().toString();
FieldError fieldError = errors.getFieldError(field); FieldError fieldError = errors.getFieldError(field);
if (fieldError == null || !fieldError.isBindingFailure()) { if (fieldError == null || !fieldError.isBindingFailure()) {

View File

@ -19,11 +19,11 @@ package org.springframework.web.servlet.mvc.method.annotation.support;
import java.lang.annotation.Annotation; import java.lang.annotation.Annotation;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import org.springframework.core.GenericCollectionTypeResolver; import org.springframework.core.GenericCollectionTypeResolver;
import org.springframework.core.MethodParameter; import org.springframework.core.MethodParameter;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.http.HttpInputMessage; import org.springframework.http.HttpInputMessage;
import org.springframework.http.converter.HttpMessageConverter; import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.util.Assert; import org.springframework.util.Assert;
@ -106,14 +106,12 @@ public class RequestPartMethodArgumentResolver extends AbstractMessageConverterM
} }
} }
public Object resolveArgument(MethodParameter parameter, public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer,
ModelAndViewContainer mavContainer, NativeWebRequest request, WebDataBinderFactory binderFactory) throws Exception {
NativeWebRequest request,
WebDataBinderFactory binderFactory) throws Exception {
HttpServletRequest servletRequest = request.getNativeRequest(HttpServletRequest.class); HttpServletRequest servletRequest = request.getNativeRequest(HttpServletRequest.class);
if (!isMultipartRequest(servletRequest)) { if (!isMultipartRequest(servletRequest)) {
throw new MultipartException("The current request is not a multipart request."); throw new MultipartException("The current request is not a multipart request");
} }
MultipartHttpServletRequest multipartRequest = MultipartHttpServletRequest multipartRequest =
@ -137,15 +135,19 @@ public class RequestPartMethodArgumentResolver extends AbstractMessageConverterM
try { try {
HttpInputMessage inputMessage = new RequestPartServletServerHttpRequest(servletRequest, partName); HttpInputMessage inputMessage = new RequestPartServletServerHttpRequest(servletRequest, partName);
arg = readWithMessageConverters(inputMessage, parameter, parameter.getParameterType()); arg = readWithMessageConverters(inputMessage, parameter, parameter.getParameterType());
if (isValidationApplicable(arg, parameter)) { Annotation[] annotations = parameter.getParameterAnnotations();
WebDataBinder binder = binderFactory.createBinder(request, arg, partName); for (Annotation annot : annotations) {
binder.validate(); if ("Valid".equals(annot.annotationType().getSimpleName())) {
BindingResult bindingResult = binder.getBindingResult(); WebDataBinder binder = binderFactory.createBinder(request, arg, partName);
if (bindingResult.hasErrors()) { Object hints = AnnotationUtils.getValue(annot);
throw new MethodArgumentNotValidException(parameter, bindingResult); binder.validate(hints instanceof Object[] ? (Object[]) hints : new Object[] {hints});
BindingResult bindingResult = binder.getBindingResult();
if (bindingResult.hasErrors()) {
throw new MethodArgumentNotValidException(parameter, bindingResult);
}
} }
} }
} }
catch (MissingServletRequestPartException e) { catch (MissingServletRequestPartException e) {
// handled below // handled below
arg = null; arg = null;
@ -153,7 +155,7 @@ public class RequestPartMethodArgumentResolver extends AbstractMessageConverterM
} }
RequestPart annot = parameter.getParameterAnnotation(RequestPart.class); RequestPart annot = parameter.getParameterAnnotation(RequestPart.class);
boolean isRequired = (annot != null) ? annot.required() : true; boolean isRequired = (annot == null || annot.required());
if (arg == null && isRequired) { if (arg == null && isRequired) {
throw new MissingServletRequestPartException(partName); throw new MissingServletRequestPartException(partName);

View File

@ -22,6 +22,7 @@ import java.util.List;
import org.springframework.core.Conventions; import org.springframework.core.Conventions;
import org.springframework.core.MethodParameter; import org.springframework.core.MethodParameter;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.http.converter.HttpMessageConverter; import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.validation.BindingResult; import org.springframework.validation.BindingResult;
import org.springframework.web.HttpMediaTypeNotAcceptableException; import org.springframework.web.HttpMediaTypeNotAcceptableException;
@ -64,43 +65,30 @@ public class RequestResponseBodyMethodProcessor extends AbstractMessageConverter
return returnType.getMethodAnnotation(ResponseBody.class) != null; return returnType.getMethodAnnotation(ResponseBody.class) != null;
} }
public Object resolveArgument(MethodParameter parameter, public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer,
ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
NativeWebRequest webRequest,
WebDataBinderFactory binderFactory) throws Exception {
Object arg = readWithMessageConverters(webRequest, parameter, parameter.getParameterType()); Object arg = readWithMessageConverters(webRequest, parameter, parameter.getParameterType());
if (isValidationApplicable(arg, parameter)) { Annotation[] annotations = parameter.getParameterAnnotations();
String name = Conventions.getVariableNameForParameter(parameter); for (Annotation annot : annotations) {
WebDataBinder binder = binderFactory.createBinder(webRequest, arg, name); if ("Valid".equals(annot.annotationType().getSimpleName())) {
binder.validate(); String name = Conventions.getVariableNameForParameter(parameter);
BindingResult bindingResult = binder.getBindingResult(); WebDataBinder binder = binderFactory.createBinder(webRequest, arg, name);
if (bindingResult.hasErrors()) { Object hints = AnnotationUtils.getValue(annot);
throw new MethodArgumentNotValidException(parameter, bindingResult); binder.validate(hints instanceof Object[] ? (Object[]) hints : new Object[] {hints});
BindingResult bindingResult = binder.getBindingResult();
if (bindingResult.hasErrors()) {
throw new MethodArgumentNotValidException(parameter, bindingResult);
}
} }
} }
return arg; return arg;
} }
/** public void handleReturnValue(Object returnValue, MethodParameter returnType,
* Whether to validate the given {@code @RequestBody} method argument. ModelAndViewContainer mavContainer, NativeWebRequest webRequest)
* The default implementation looks for {@code @javax.validation.Valid}. throws IOException, HttpMediaTypeNotAcceptableException {
* @param argument the resolved argument value
* @param parameter the method argument
*/
protected boolean isValidationApplicable(Object argument, MethodParameter parameter) {
Annotation[] annotations = parameter.getParameterAnnotations();
for (Annotation annot : annotations) {
if ("Valid".equals(annot.annotationType().getSimpleName())) {
return true;
}
}
return false;
}
public void handleReturnValue(Object returnValue,
MethodParameter returnType,
ModelAndViewContainer mavContainer,
NativeWebRequest webRequest) throws IOException, HttpMediaTypeNotAcceptableException {
mavContainer.setRequestHandled(true); mavContainer.setRequestHandled(true);
if (returnValue != null) { if (returnValue != null) {
writeWithMessageConverters(returnValue, returnType, webRequest); writeWithMessageConverters(returnValue, returnType, webRequest);

View File

@ -16,24 +16,18 @@
package org.springframework.web.servlet.config; package org.springframework.web.servlet.config;
import static org.junit.Assert.assertEquals; import java.lang.annotation.Retention;
import static org.junit.Assert.assertFalse; import java.lang.annotation.RetentionPolicy;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import javax.servlet.RequestDispatcher; import javax.servlet.RequestDispatcher;
import javax.validation.Valid;
import javax.validation.constraints.NotNull; import javax.validation.constraints.NotNull;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.springframework.beans.DirectFieldAccessor; import org.springframework.beans.DirectFieldAccessor;
import org.springframework.beans.TypeMismatchException; import org.springframework.beans.TypeMismatchException;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader; import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
@ -53,6 +47,7 @@ import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult; import org.springframework.validation.BindingResult;
import org.springframework.validation.Errors; import org.springframework.validation.Errors;
import org.springframework.validation.Validator; import org.springframework.validation.Validator;
import org.springframework.validation.annotation.Valid;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean; import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RequestParam;
@ -75,6 +70,8 @@ import org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler
import org.springframework.web.servlet.resource.ResourceHttpRequestHandler; import org.springframework.web.servlet.resource.ResourceHttpRequestHandler;
import org.springframework.web.servlet.theme.ThemeChangeInterceptor; import org.springframework.web.servlet.theme.ThemeChangeInterceptor;
import static org.junit.Assert.*;
/** /**
* @author Keith Donald * @author Keith Donald
* @author Arjen Poutsma * @author Arjen Poutsma
@ -453,12 +450,8 @@ public class MvcNamespaceTests {
private boolean recordedValidationError; private boolean recordedValidationError;
@RequestMapping @RequestMapping
public void testBind(@RequestParam @DateTimeFormat(iso=ISO.DATE) Date date, @Valid TestBean bean, BindingResult result) { public void testBind(@RequestParam @DateTimeFormat(iso=ISO.DATE) Date date, @Valid(MyGroup.class) TestBean bean, BindingResult result) {
if (result.getErrorCount() == 1) { this.recordedValidationError = (result.getErrorCount() == 1);
this.recordedValidationError = true;
} else {
this.recordedValidationError = false;
}
} }
} }
@ -475,9 +468,13 @@ public class MvcNamespaceTests {
} }
} }
@Retention(RetentionPolicy.RUNTIME)
public @interface MyGroup {
}
private static class TestBean { private static class TestBean {
@NotNull @NotNull(groups=MyGroup.class)
private String field; private String field;
@SuppressWarnings("unused") @SuppressWarnings("unused")

View File

@ -16,14 +16,6 @@
package org.springframework.web.servlet.mvc.annotation; package org.springframework.web.servlet.mvc.annotation;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.beans.PropertyEditorSupport; import java.beans.PropertyEditorSupport;
import java.io.IOException; import java.io.IOException;
import java.io.Serializable; import java.io.Serializable;
@ -38,16 +30,17 @@ import java.security.Principal;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.Date; import java.util.Date;
import java.util.HashSet; import java.util.HashSet;
import java.util.Iterator; import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import javax.servlet.ServletConfig; import javax.servlet.ServletConfig;
import javax.servlet.ServletContext; import javax.servlet.ServletContext;
import javax.servlet.ServletException; import javax.servlet.ServletException;
@ -55,11 +48,11 @@ import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession; import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import javax.validation.constraints.NotNull; import javax.validation.constraints.NotNull;
import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlRootElement;
import org.junit.Test; import org.junit.Test;
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator; import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
import org.springframework.aop.interceptor.SimpleTraceInterceptor; import org.springframework.aop.interceptor.SimpleTraceInterceptor;
import org.springframework.aop.support.DefaultPointcutAdvisor; import org.springframework.aop.support.DefaultPointcutAdvisor;
@ -67,6 +60,8 @@ import org.springframework.beans.BeansException;
import org.springframework.beans.DerivedTestBean; import org.springframework.beans.DerivedTestBean;
import org.springframework.beans.GenericBean; import org.springframework.beans.GenericBean;
import org.springframework.beans.ITestBean; import org.springframework.beans.ITestBean;
import org.springframework.beans.PropertyEditorRegistrar;
import org.springframework.beans.PropertyEditorRegistry;
import org.springframework.beans.TestBean; import org.springframework.beans.TestBean;
import org.springframework.beans.factory.BeanCreationException; import org.springframework.beans.factory.BeanCreationException;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@ -108,6 +103,7 @@ import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult; import org.springframework.validation.BindingResult;
import org.springframework.validation.Errors; import org.springframework.validation.Errors;
import org.springframework.validation.FieldError; import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Valid;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean; import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
import org.springframework.web.bind.WebDataBinder; import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.CookieValue; import org.springframework.web.bind.annotation.CookieValue;
@ -142,6 +138,8 @@ import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springframework.web.servlet.view.InternalResourceViewResolver; import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.util.NestedServletException; import org.springframework.web.util.NestedServletException;
import static org.junit.Assert.*;
/** /**
* @author Juergen Hoeller * @author Juergen Hoeller
* @author Sam Brannen * @author Sam Brannen
@ -1718,6 +1716,61 @@ public class ServletAnnotationControllerTests {
assertEquals("templatePath", response.getContentAsString()); assertEquals("templatePath", response.getContentAsString());
} }
@Test
public void testMatchWithoutMethodLevelPath() throws Exception {
initServlet(NoPathGetAndM2PostController.class);
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/t1/m2");
MockHttpServletResponse response = new MockHttpServletResponse();
servlet.service(request, response);
assertEquals(405, response.getStatus());
}
// SPR-8536
@Test
public void testHeadersCondition() throws Exception {
initServlet(HeadersConditionController.class);
// No "Accept" header
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/");
MockHttpServletResponse response = new MockHttpServletResponse();
servlet.service(request, response);
assertEquals(200, response.getStatus());
assertEquals("home", response.getForwardedUrl());
// Accept "*/*"
request = new MockHttpServletRequest("GET", "/");
request.addHeader("Accept", "*/*");
response = new MockHttpServletResponse();
servlet.service(request, response);
assertEquals(200, response.getStatus());
assertEquals("home", response.getForwardedUrl());
// Accept "application/json"
request = new MockHttpServletRequest("GET", "/");
request.addHeader("Accept", "application/json");
response = new MockHttpServletResponse();
servlet.service(request, response);
assertEquals(200, response.getStatus());
assertEquals("application/json", response.getHeader("Content-Type"));
assertEquals("homeJson", response.getContentAsString());
}
@Test
public void redirectAttribute() throws Exception {
initServlet(RedirectAttributesController.class);
try {
servlet.service(new MockHttpServletRequest("GET", "/"), new MockHttpServletResponse());
}
catch (NestedServletException ex) {
assertTrue(ex.getMessage().contains("not assignable from the actual model"));
}
}
/* /*
* See SPR-6021 * See SPR-6021
*/ */
@ -1868,60 +1921,59 @@ public class ServletAnnotationControllerTests {
} }
@Test @Test
public void testMatchWithoutMethodLevelPath() throws Exception { public void parameterCsvAsIntegerSetWithCustomSeparator() throws Exception {
initServlet(NoPathGetAndM2PostController.class); servlet = new DispatcherServlet() {
@Override
protected WebApplicationContext createWebApplicationContext(WebApplicationContext parent) {
GenericWebApplicationContext wac = new GenericWebApplicationContext();
wac.registerBeanDefinition("controller", new RootBeanDefinition(CsvController.class));
RootBeanDefinition csDef = new RootBeanDefinition(FormattingConversionServiceFactoryBean.class);
RootBeanDefinition wbiDef = new RootBeanDefinition(ConfigurableWebBindingInitializer.class);
wbiDef.getPropertyValues().add("conversionService", csDef);
wbiDef.getPropertyValues().add("propertyEditorRegistrars", new RootBeanDefinition(ListEditorRegistrar.class));
RootBeanDefinition adapterDef = new RootBeanDefinition(AnnotationMethodHandlerAdapter.class);
adapterDef.getPropertyValues().add("webBindingInitializer", wbiDef);
wac.registerBeanDefinition("handlerAdapter", adapterDef);
wac.refresh();
return wac;
}
};
servlet.init(new MockServletConfig());
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/t1/m2"); MockHttpServletRequest request = new MockHttpServletRequest();
request.setRequestURI("/integerSet");
request.setMethod("POST");
request.addParameter("content", "1;2");
MockHttpServletResponse response = new MockHttpServletResponse(); MockHttpServletResponse response = new MockHttpServletResponse();
servlet.service(request, response); servlet.service(request, response);
assertEquals(405, response.getStatus()); assertEquals("1-2", response.getContentAsString());
} }
// SPR-8536
@Test public static class ListEditorRegistrar implements PropertyEditorRegistrar {
public void testHeadersCondition() throws Exception {
initServlet(HeadersConditionController.class);
// No "Accept" header public void registerCustomEditors(PropertyEditorRegistry registry) {
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/"); registry.registerCustomEditor(Set.class, new ListEditor());
MockHttpServletResponse response = new MockHttpServletResponse();
servlet.service(request, response);
assertEquals(200, response.getStatus());
assertEquals("home", response.getForwardedUrl());
// Accept "*/*"
request = new MockHttpServletRequest("GET", "/");
request.addHeader("Accept", "*/*");
response = new MockHttpServletResponse();
servlet.service(request, response);
assertEquals(200, response.getStatus());
assertEquals("home", response.getForwardedUrl());
// Accept "application/json"
request = new MockHttpServletRequest("GET", "/");
request.addHeader("Accept", "application/json");
response = new MockHttpServletResponse();
servlet.service(request, response);
assertEquals(200, response.getStatus());
assertEquals("application/json", response.getHeader("Content-Type"));
assertEquals("homeJson", response.getContentAsString());
}
@Test
public void redirectAttribute() throws Exception {
initServlet(RedirectAttributesController.class);
try {
servlet.service(new MockHttpServletRequest("GET", "/"), new MockHttpServletResponse());
}
catch (NestedServletException ex) {
assertTrue(ex.getMessage().contains("not assignable from the actual model"));
} }
} }
public static class ListEditor extends PropertyEditorSupport {
@SuppressWarnings("unchecked")
@Override
public String getAsText() {
return StringUtils.collectionToDelimitedString((Collection<String>) getValue(), ";");
}
@Override
public void setAsText(String text) throws IllegalArgumentException {
Set<String> s = new LinkedHashSet<String>();
for (String t : text.split(";")) {
s.add(t);
}
setValue(s);
}
}
/* /*
* Controllers * Controllers
@ -2226,7 +2278,7 @@ public class ServletAnnotationControllerTests {
public static class ValidTestBean extends TestBean { public static class ValidTestBean extends TestBean {
@NotNull @NotNull(groups = MyGroup.class)
private String validCountry; private String validCountry;
public void setValidCountry(String validCountry) { public void setValidCountry(String validCountry) {
@ -2264,9 +2316,7 @@ public class ServletAnnotationControllerTests {
@SuppressWarnings("unused") @SuppressWarnings("unused")
@ModelAttribute("myCommand") @ModelAttribute("myCommand")
private ValidTestBean createTestBean(@RequestParam T defaultName, private ValidTestBean createTestBean(@RequestParam T defaultName, Map<String, Object> model, @RequestParam Date date) {
Map<String, Object> model,
@RequestParam Date date) {
model.put("myKey", "myOriginalValue"); model.put("myKey", "myOriginalValue");
ValidTestBean tb = new ValidTestBean(); ValidTestBean tb = new ValidTestBean();
tb.setName(defaultName.getClass().getSimpleName() + ":" + defaultName.toString()); tb.setName(defaultName.getClass().getSimpleName() + ":" + defaultName.toString());
@ -2275,7 +2325,7 @@ public class ServletAnnotationControllerTests {
@Override @Override
@RequestMapping("/myPath.do") @RequestMapping("/myPath.do")
public String myHandle(@ModelAttribute("myCommand") @Valid TestBean tb, BindingResult errors, ModelMap model) { public String myHandle(@ModelAttribute("myCommand") @Valid(MyGroup.class) TestBean tb, BindingResult errors, ModelMap model) {
if (!errors.hasFieldErrors("validCountry")) { if (!errors.hasFieldErrors("validCountry")) {
throw new IllegalStateException("Declarative validation not applied"); throw new IllegalStateException("Declarative validation not applied");
} }
@ -2333,7 +2383,7 @@ public class ServletAnnotationControllerTests {
@Override @Override
@RequestMapping("/myPath.do") @RequestMapping("/myPath.do")
public String myHandle(@ModelAttribute("myCommand") @Valid TestBean tb, BindingResult errors, ModelMap model) { public String myHandle(@ModelAttribute("myCommand") @Valid(MyGroup.class) TestBean tb, BindingResult errors, ModelMap model) {
if (!errors.hasFieldErrors("sex")) { if (!errors.hasFieldErrors("sex")) {
throw new IllegalStateException("requiredFields not applied"); throw new IllegalStateException("requiredFields not applied");
} }
@ -2360,6 +2410,10 @@ public class ServletAnnotationControllerTests {
} }
} }
@Retention(RetentionPolicy.RUNTIME)
public @interface MyGroup {
}
private static class MyWebBindingInitializer implements WebBindingInitializer { private static class MyWebBindingInitializer implements WebBindingInitializer {
public void initBinder(WebDataBinder binder, WebRequest request) { public void initBinder(WebDataBinder binder, WebRequest request) {

View File

@ -34,6 +34,7 @@ import java.util.Set;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeanUtils; import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.BridgeMethodResolver; import org.springframework.core.BridgeMethodResolver;
@ -251,6 +252,7 @@ public class HandlerMethodInvoker {
boolean required = false; boolean required = false;
String defaultValue = null; String defaultValue = null;
boolean validate = false; boolean validate = false;
Object[] validationHints = null;
int annotationsFound = 0; int annotationsFound = 0;
Annotation[] paramAnns = methodParam.getParameterAnnotations(); Annotation[] paramAnns = methodParam.getParameterAnnotations();
@ -295,6 +297,8 @@ public class HandlerMethodInvoker {
} }
else if ("Valid".equals(paramAnn.annotationType().getSimpleName())) { else if ("Valid".equals(paramAnn.annotationType().getSimpleName())) {
validate = true; validate = true;
Object value = AnnotationUtils.getValue(paramAnn);
validationHints = (value instanceof Object[] ? (Object[]) value : new Object[] {value});
} }
} }
@ -360,7 +364,7 @@ public class HandlerMethodInvoker {
resolveModelAttribute(attrName, methodParam, implicitModel, webRequest, handler); resolveModelAttribute(attrName, methodParam, implicitModel, webRequest, handler);
boolean assignBindingResult = (args.length > i + 1 && Errors.class.isAssignableFrom(paramTypes[i + 1])); boolean assignBindingResult = (args.length > i + 1 && Errors.class.isAssignableFrom(paramTypes[i + 1]));
if (binder.getTarget() != null) { if (binder.getTarget() != null) {
doBind(binder, webRequest, validate, !assignBindingResult); doBind(binder, webRequest, validate, validationHints, !assignBindingResult);
} }
args[i] = binder.getTarget(); args[i] = binder.getTarget();
if (assignBindingResult) { if (assignBindingResult) {
@ -803,12 +807,12 @@ public class HandlerMethodInvoker {
return new WebRequestDataBinder(target, objectName); return new WebRequestDataBinder(target, objectName);
} }
private void doBind(WebDataBinder binder, NativeWebRequest webRequest, boolean validate, boolean failOnErrors) private void doBind(WebDataBinder binder, NativeWebRequest webRequest, boolean validate,
throws Exception { Object[] validationHints, boolean failOnErrors) throws Exception {
doBind(binder, webRequest); doBind(binder, webRequest);
if (validate) { if (validate) {
binder.validate(); binder.validate(validationHints);
} }
if (failOnErrors && binder.getBindingResult().hasErrors()) { if (failOnErrors && binder.getBindingResult().hasErrors()) {
throw new BindException(binder.getBindingResult()); throw new BindException(binder.getBindingResult());

View File

@ -140,16 +140,6 @@ public class ModelAttributeMethodProcessorTests {
assertTrue(processor.supportsReturnType(returnParamNonSimpleType)); assertTrue(processor.supportsReturnType(returnParamNonSimpleType));
} }
@Test
public void validationApplicable() throws Exception {
assertTrue(processor.isValidationApplicable(null, paramNamedValidModelAttr));
}
@Test
public void validationNotApplicable() throws Exception {
assertFalse(processor.isValidationApplicable(null, paramNonSimpleType));
}
@Test @Test
public void bindExceptionRequired() throws Exception { public void bindExceptionRequired() throws Exception {
assertTrue(processor.isBindExceptionRequired(null, paramNonSimpleType)); assertTrue(processor.isBindExceptionRequired(null, paramNonSimpleType));