SPR-6709 Support @Valid with @RequestBody method arguments

This commit is contained in:
Rossen Stoyanchev
2011-06-02 17:21:44 +00:00
parent 14edc9fc03
commit 18f5d90235
3 changed files with 178 additions and 15 deletions

View File

@@ -0,0 +1,48 @@
/*
* 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.web.servlet.mvc.method.annotation.support;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.RequestBody;
/**
* Thrown by {@link RequestResponseBodyMethodProcessor} when an @{@link RequestBody} argument annotated with
* {@code @Valid} results in validation errors.
*
* @author Rossen Stoyanchev
* @since 3.1
*/
@SuppressWarnings("serial")
public class RequestBodyNotValidException extends RuntimeException {
private final Errors errors;
/**
* @param errors contains the results of validating an @{@link RequestBody} argument.
*/
public RequestBodyNotValidException(Errors errors) {
this.errors = errors;
}
/**
* Returns an Errors instance with validation errors.
*/
public Errors getErrors() {
return errors;
}
}

View File

@@ -17,21 +17,30 @@
package org.springframework.web.servlet.mvc.method.annotation.support;
import java.io.IOException;
import java.lang.annotation.Annotation;
import java.util.List;
import org.springframework.core.Conventions;
import org.springframework.core.MethodParameter;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;
import org.springframework.web.HttpMediaTypeNotAcceptableException;
import org.springframework.web.HttpMediaTypeNotSupportedException;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.ModelAndViewContainer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
/**
* Resolves method arguments annotated with @{@link RequestBody}. Handles return values from methods annotated with
* {@link ResponseBody}.
* Resolves method arguments annotated with @{@link RequestBody} and handles return values from methods
* annotated with {@link ResponseBody}.
*
* <p>An @{@link RequestBody} method argument will be validated if annotated with {@code @Valid}. A
* {@link Validator} instance can be configured globally in XML configuration with the Spring MVC namespace
* or in Java-based configuration with @{@link EnableWebMvc}.
*
* @author Arjen Poutsma
* @author Rossen Stoyanchev
@@ -54,11 +63,37 @@ public class RequestResponseBodyMethodProcessor extends AbstractMessageConverter
public Object resolveArgument(MethodParameter parameter,
ModelAndViewContainer mavContainer,
NativeWebRequest webRequest,
WebDataBinderFactory binderFactory)
throws IOException, HttpMediaTypeNotSupportedException {
return readWithMessageConverters(webRequest, parameter, parameter.getParameterType());
WebDataBinderFactory binderFactory) throws Exception {
Object arg = readWithMessageConverters(webRequest, parameter, parameter.getParameterType());
if (shouldValidate(parameter, arg)) {
String argName = Conventions.getVariableNameForParameter(parameter);
WebDataBinder binder = binderFactory.createBinder(webRequest, arg, argName);
binder.validate();
Errors errors = binder.getBindingResult();
if (errors.hasErrors()) {
throw new RequestBodyNotValidException(errors);
}
}
return arg;
}
/**
* Whether to validate the given @{@link RequestBody} method argument. The default implementation checks
* if the parameter is also annotated with {@code @Valid}.
* @param parameter the method argument for which to check if validation is needed
* @param argumentValue the method argument value (instantiated with a message converter)
* @return {@code true} if validation should be invoked, {@code false} otherwise.
*/
protected boolean shouldValidate(MethodParameter parameter, Object argumentValue) {
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,