Add default web handling of method validation errors
Closes gh-30644
This commit is contained in:
@@ -43,7 +43,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.WebDataBinder;
|
||||
@@ -55,6 +54,7 @@ import org.springframework.web.bind.support.ConfigurableWebBindingInitializer;
|
||||
import org.springframework.web.bind.support.WebExchangeBindException;
|
||||
import org.springframework.web.context.support.GenericWebApplicationContext;
|
||||
import org.springframework.web.method.HandlerMethod;
|
||||
import org.springframework.web.method.annotation.HandlerMethodValidationException;
|
||||
import org.springframework.web.reactive.HandlerResult;
|
||||
import org.springframework.web.server.ServerWebExchange;
|
||||
import org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest;
|
||||
@@ -202,7 +202,7 @@ public class MethodValidationTests {
|
||||
|
||||
StepVerifier.create(this.handlerAdapter.handle(exchange, hm))
|
||||
.consumeErrorWith(throwable -> {
|
||||
MethodValidationException ex = (MethodValidationException) throwable;
|
||||
HandlerMethodValidationException ex = (HandlerMethodValidationException) throwable;
|
||||
|
||||
assertThat(this.jakartaValidator.getValidationCount()).isEqualTo(1);
|
||||
assertThat(this.jakartaValidator.getMethodValidationCount()).isEqualTo(1);
|
||||
|
||||
@@ -37,9 +37,12 @@ import org.springframework.http.ProblemDetail;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.util.LinkedMultiValueMap;
|
||||
import org.springframework.validation.BeanPropertyBindingResult;
|
||||
import org.springframework.validation.beanvalidation.MethodValidationException;
|
||||
import org.springframework.validation.beanvalidation.MethodValidationResult;
|
||||
import org.springframework.web.ErrorResponse;
|
||||
import org.springframework.web.ErrorResponseException;
|
||||
import org.springframework.web.bind.support.WebExchangeBindException;
|
||||
import org.springframework.web.method.annotation.HandlerMethodValidationException;
|
||||
import org.springframework.web.server.MethodNotAllowedException;
|
||||
import org.springframework.web.server.MissingRequestValueException;
|
||||
import org.springframework.web.server.NotAcceptableStatusException;
|
||||
@@ -53,6 +56,7 @@ import org.springframework.web.testfixture.http.server.reactive.MockServerHttpRe
|
||||
import org.springframework.web.testfixture.server.MockServerWebExchange;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.BDDMockito.mock;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link ResponseEntityExceptionHandler}.
|
||||
@@ -105,6 +109,21 @@ public class ResponseEntityExceptionHandlerTests {
|
||||
testException(new WebExchangeBindException(null, new BeanPropertyBindingResult(new Object(), "foo")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void handlerMethodValidationException() {
|
||||
testException(new HandlerMethodValidationException(mock(MethodValidationResult.class)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void methodValidationException() {
|
||||
MethodValidationException ex = new MethodValidationException(mock(MethodValidationResult.class));
|
||||
ResponseEntity<?> entity = this.exceptionHandler.handleException(ex, this.exchange).block();
|
||||
|
||||
assertThat(entity).isNotNull();
|
||||
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR);
|
||||
assertThat(entity.getBody()).isInstanceOf(ProblemDetail.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
void handleServerWebInputException() {
|
||||
testException(new ServerWebInputException(""));
|
||||
|
||||
Reference in New Issue
Block a user