From f1ad53d570b407a88665560c1e4adc61a21d6ab8 Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Fri, 3 Jun 2011 09:38:22 +0000 Subject: [PATCH] SPR-6709 Update changelog and add one test --- .../resources/changelog.txt | 1 + .../DefaultHandlerExceptionResolver.java | 3 ++- .../DefaultHandlerExceptionResolverTests.java | 21 +++++++++++++++++-- .../web/bind/annotation/RequestMapping.java | 2 +- 4 files changed, 23 insertions(+), 4 deletions(-) diff --git a/build-spring-framework/resources/changelog.txt b/build-spring-framework/resources/changelog.txt index 36478c23200..412c9b83114 100644 --- a/build-spring-framework/resources/changelog.txt +++ b/build-spring-framework/resources/changelog.txt @@ -17,6 +17,7 @@ Changes in version 3.1 M2 (2011-05-31) * support for including @PathVariables in data binding * support for URI template variables in view names with the "redirect:" prefix * added a flag for extracting the value from single-key models in MappingJacksonJsonView +* added support for @Valid with @RequestBody arguments * allow bean references in mvc:interceptor namespace elements * consolidated the initialization and use of MappedInterceptors in AbstractHandlerMapping * added Servlet 3.0 based WebApplicationInitializer mechanism for programmatic bootstrapping diff --git a/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/mvc/support/DefaultHandlerExceptionResolver.java b/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/mvc/support/DefaultHandlerExceptionResolver.java index 9589e65438c..07088f12e0b 100644 --- a/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/mvc/support/DefaultHandlerExceptionResolver.java +++ b/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/mvc/support/DefaultHandlerExceptionResolver.java @@ -49,6 +49,7 @@ import org.springframework.web.servlet.mvc.multiaction.NoSuchRequestHandlingMeth *

This exception resolver is enabled by default in the {@link org.springframework.web.servlet.DispatcherServlet}. * * @author Arjen Poutsma + * @author Rossen Stoyanchev * @since 3.0 * @see #handleNoSuchRequestHandlingMethod * @see #handleHttpRequestMethodNotSupported @@ -321,7 +322,7 @@ public class DefaultHandlerExceptionResolver extends AbstractHandlerExceptionRes /** * Handle the case where the object created from the body of a request has failed validation. The default - * implementation sends an HTTP 500 error along with a message containing the errors. + * implementation sends an HTTP 400 error along with a message containing the errors. * @param request current HTTP request * @param response current HTTP response * @param handler the executed handler, or null if none chosen diff --git a/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/mvc/support/DefaultHandlerExceptionResolverTests.java b/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/mvc/support/DefaultHandlerExceptionResolverTests.java index b49b3499899..c18387933ba 100644 --- a/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/mvc/support/DefaultHandlerExceptionResolverTests.java +++ b/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/mvc/support/DefaultHandlerExceptionResolverTests.java @@ -16,22 +16,27 @@ package org.springframework.web.servlet.mvc.support; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + import java.util.Collections; -import static org.junit.Assert.*; import org.junit.Before; import org.junit.Test; - +import org.springframework.beans.TestBean; import org.springframework.beans.TypeMismatchException; import org.springframework.http.MediaType; import org.springframework.http.converter.HttpMessageNotReadableException; import org.springframework.http.converter.HttpMessageNotWritableException; import org.springframework.mock.web.MockHttpServletRequest; import org.springframework.mock.web.MockHttpServletResponse; +import org.springframework.validation.BeanPropertyBindingResult; import org.springframework.web.HttpMediaTypeNotSupportedException; import org.springframework.web.HttpRequestMethodNotSupportedException; import org.springframework.web.bind.MissingServletRequestParameterException; import org.springframework.web.servlet.ModelAndView; +import org.springframework.web.servlet.mvc.method.annotation.support.RequestBodyNotValidException; import org.springframework.web.servlet.mvc.multiaction.NoSuchRequestHandlingMethodException; /** @author Arjen Poutsma */ @@ -118,5 +123,17 @@ public class DefaultHandlerExceptionResolverTests { assertEquals("Invalid status code", 500, response.getStatus()); } + @Test + public void handleRequestBodyNotValid() { + BeanPropertyBindingResult errors = new BeanPropertyBindingResult(new TestBean(), "testBean"); + errors.rejectValue("name", "invalid"); + RequestBodyNotValidException ex = new RequestBodyNotValidException(errors); + ModelAndView mav = exceptionResolver.resolveException(request, response, null, ex); + assertNotNull("No ModelAndView returned", mav); + assertTrue("No Empty ModelAndView returned", mav.isEmpty()); + assertEquals("Invalid status code", 400, response.getStatus()); + assertTrue(response.getErrorMessage().startsWith("Request body content validation failed")); + assertTrue(response.getErrorMessage().contains("Field error in object 'testBean' on field 'name'")); + } } diff --git a/org.springframework.web/src/main/java/org/springframework/web/bind/annotation/RequestMapping.java b/org.springframework.web/src/main/java/org/springframework/web/bind/annotation/RequestMapping.java index 59adb29a117..b8b1c87398c 100644 --- a/org.springframework.web/src/main/java/org/springframework/web/bind/annotation/RequestMapping.java +++ b/org.springframework.web/src/main/java/org/springframework/web/bind/annotation/RequestMapping.java @@ -94,7 +94,7 @@ import java.lang.annotation.Target; * the Servlet request HTTP contents. The request stream will be * converted to the declared method argument type using * {@linkplain org.springframework.http.converter.HttpMessageConverter message - * converters}. + * converters}. Such parameters may optionally be annotated with {@code @Valid}. *

  • {@link org.springframework.http.HttpEntity HttpEntity<?>} parameters * for access to the Servlet request HTTP headers and contents. The request stream will be * converted to the entity body using