Replace RequestBodyNotValidException and RequestPartNotValidException with MethodArgumentNotValidException and add MethodParameter information to the exception message.
This commit is contained in:
@@ -59,6 +59,7 @@ import org.springframework.web.bind.annotation.RequestPart;
|
||||
import org.springframework.web.bind.support.WebDataBinderFactory;
|
||||
import org.springframework.web.context.request.NativeWebRequest;
|
||||
import org.springframework.web.context.request.ServletWebRequest;
|
||||
import org.springframework.web.method.annotation.support.MethodArgumentNotValidException;
|
||||
import org.springframework.web.method.support.ModelAndViewContainer;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
import org.springframework.web.multipart.support.RequestPartServletServerHttpRequest;
|
||||
@@ -194,10 +195,10 @@ public class RequestPartMethodArgumentResolverTests {
|
||||
try {
|
||||
testResolveArgument(new SimpleBean(null), paramValidRequestPart);
|
||||
fail("Expected exception");
|
||||
} catch (RequestPartNotValidException e) {
|
||||
assertEquals("requestPart", e.getErrors().getObjectName());
|
||||
assertEquals(1, e.getErrors().getErrorCount());
|
||||
assertNotNull(e.getErrors().getFieldError("name"));
|
||||
} catch (MethodArgumentNotValidException e) {
|
||||
assertEquals("requestPart", e.getBindingResult().getObjectName());
|
||||
assertEquals(1, e.getBindingResult().getErrorCount());
|
||||
assertNotNull(e.getBindingResult().getFieldError("name"));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -59,6 +59,7 @@ 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.context.request.ServletWebRequest;
|
||||
import org.springframework.web.method.annotation.support.MethodArgumentNotValidException;
|
||||
import org.springframework.web.method.support.ModelAndViewContainer;
|
||||
import org.springframework.web.servlet.HandlerMapping;
|
||||
|
||||
@@ -148,10 +149,10 @@ public class RequestResponseBodyMethodProcessorTests {
|
||||
try {
|
||||
testResolveArgumentWithValidation(new SimpleBean(null));
|
||||
fail("Expected exception");
|
||||
} catch (RequestBodyNotValidException e) {
|
||||
assertEquals("simpleBean", e.getErrors().getObjectName());
|
||||
assertEquals(1, e.getErrors().getErrorCount());
|
||||
assertNotNull(e.getErrors().getFieldError("name"));
|
||||
} catch (MethodArgumentNotValidException e) {
|
||||
assertEquals("simpleBean", e.getBindingResult().getObjectName());
|
||||
assertEquals(1, e.getBindingResult().getErrorCount());
|
||||
assertNotNull(e.getBindingResult().getFieldError("name"));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -26,6 +26,7 @@ import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.TestBean;
|
||||
import org.springframework.beans.TypeMismatchException;
|
||||
import org.springframework.core.MethodParameter;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.converter.HttpMessageNotReadableException;
|
||||
import org.springframework.http.converter.HttpMessageNotWritableException;
|
||||
@@ -36,9 +37,8 @@ import org.springframework.web.HttpMediaTypeNotSupportedException;
|
||||
import org.springframework.web.HttpRequestMethodNotSupportedException;
|
||||
import org.springframework.web.bind.MissingServletRequestParameterException;
|
||||
import org.springframework.web.bind.ServletRequestBindingException;
|
||||
import org.springframework.web.method.annotation.support.MethodArgumentNotValidException;
|
||||
import org.springframework.web.servlet.ModelAndView;
|
||||
import org.springframework.web.servlet.mvc.method.annotation.support.RequestBodyNotValidException;
|
||||
import org.springframework.web.servlet.mvc.method.annotation.support.RequestPartNotValidException;
|
||||
import org.springframework.web.servlet.mvc.multiaction.NoSuchRequestHandlingMethodException;
|
||||
|
||||
/** @author Arjen Poutsma */
|
||||
@@ -136,24 +136,18 @@ public class DefaultHandlerExceptionResolverTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void handleRequestBodyNotValid() {
|
||||
public void handleMethodArgumentNotValid() throws Exception {
|
||||
BeanPropertyBindingResult errors = new BeanPropertyBindingResult(new TestBean(), "testBean");
|
||||
errors.rejectValue("name", "invalid");
|
||||
RequestBodyNotValidException ex = new RequestBodyNotValidException(errors);
|
||||
MethodParameter parameter = new MethodParameter(this.getClass().getMethod("handle", String.class), 0);
|
||||
MethodArgumentNotValidException ex = new MethodArgumentNotValidException(parameter, 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());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void handleRequestPartNotValid() {
|
||||
BeanPropertyBindingResult errors = new BeanPropertyBindingResult(new TestBean(), "testBean");
|
||||
errors.rejectValue("name", "invalid");
|
||||
RequestPartNotValidException ex = new RequestPartNotValidException(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());
|
||||
public void handle(String arg) {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user