Add default web handling of method validation errors
Closes gh-30644
This commit is contained in:
@@ -39,7 +39,6 @@ import org.springframework.validation.FieldError;
|
||||
import org.springframework.validation.Validator;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
|
||||
import org.springframework.validation.beanvalidation.MethodValidationException;
|
||||
import org.springframework.validation.beanvalidation.ParameterValidationResult;
|
||||
import org.springframework.validation.beanvalidation.SpringValidatorAdapter;
|
||||
import org.springframework.web.bind.MethodArgumentNotValidException;
|
||||
@@ -164,9 +163,9 @@ public class MethodValidationTests {
|
||||
this.request.addParameter("name", "name=Faustino1234");
|
||||
this.request.addHeader("myHeader", "123");
|
||||
|
||||
MethodValidationException ex = catchThrowableOfType(
|
||||
HandlerMethodValidationException ex = catchThrowableOfType(
|
||||
() -> this.handlerAdapter.handle(this.request, this.response, hm),
|
||||
MethodValidationException.class);
|
||||
HandlerMethodValidationException.class);
|
||||
|
||||
assertThat(this.jakartaValidator.getValidationCount()).isEqualTo(1);
|
||||
assertThat(this.jakartaValidator.getMethodValidationCount()).isEqualTo(1);
|
||||
|
||||
@@ -43,6 +43,8 @@ import org.springframework.http.converter.HttpMessageNotWritableException;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.validation.BindException;
|
||||
import org.springframework.validation.MapBindingResult;
|
||||
import org.springframework.validation.beanvalidation.MethodValidationException;
|
||||
import org.springframework.validation.beanvalidation.MethodValidationResult;
|
||||
import org.springframework.web.ErrorResponse;
|
||||
import org.springframework.web.HttpMediaTypeNotAcceptableException;
|
||||
import org.springframework.web.HttpMediaTypeNotSupportedException;
|
||||
@@ -58,6 +60,7 @@ import org.springframework.web.context.request.ServletWebRequest;
|
||||
import org.springframework.web.context.request.WebRequest;
|
||||
import org.springframework.web.context.request.async.AsyncRequestTimeoutException;
|
||||
import org.springframework.web.context.support.StaticWebApplicationContext;
|
||||
import org.springframework.web.method.annotation.HandlerMethodValidationException;
|
||||
import org.springframework.web.multipart.support.MissingServletRequestPartException;
|
||||
import org.springframework.web.servlet.DispatcherServlet;
|
||||
import org.springframework.web.servlet.ModelAndView;
|
||||
@@ -69,6 +72,7 @@ import org.springframework.web.testfixture.servlet.MockHttpServletResponse;
|
||||
import org.springframework.web.testfixture.servlet.MockServletConfig;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.BDDMockito.mock;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link ResponseEntityExceptionHandler}.
|
||||
@@ -245,6 +249,16 @@ public class ResponseEntityExceptionHandlerTests {
|
||||
new MapBindingResult(Collections.emptyMap(), "name")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void handlerMethodValidationException() {
|
||||
testException(new HandlerMethodValidationException(mock(MethodValidationResult.class)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void methodValidationException() {
|
||||
testException(new MethodValidationException(mock(MethodValidationResult.class)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void missingServletRequestPart() {
|
||||
testException(new MissingServletRequestPartException("partName"));
|
||||
@@ -351,6 +365,7 @@ public class ResponseEntityExceptionHandlerTests {
|
||||
private ResponseEntity<Object> testException(Exception ex) {
|
||||
try {
|
||||
ResponseEntity<Object> entity = this.exceptionHandler.handleException(ex, this.request);
|
||||
assertThat(entity).isNotNull();
|
||||
|
||||
// SPR-9653
|
||||
if (HttpStatus.INTERNAL_SERVER_ERROR.equals(entity.getStatusCode())) {
|
||||
@@ -383,7 +398,7 @@ public class ResponseEntityExceptionHandlerTests {
|
||||
private static class NestedExceptionThrowingController {
|
||||
|
||||
@RequestMapping("/")
|
||||
public void handleRequest() throws Exception {
|
||||
public void handleRequest() {
|
||||
throw new IllegalStateException(new ServletRequestBindingException("message"));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user