diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/reactive/error/DefaultErrorAttributes.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/reactive/error/DefaultErrorAttributes.java index ef6d8971ba..d57ac0ecfe 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/reactive/error/DefaultErrorAttributes.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/reactive/error/DefaultErrorAttributes.java @@ -122,6 +122,9 @@ public class DefaultErrorAttributes implements ErrorAttributes { else if (error instanceof ResponseStatusException responseStatusException) { errorAttributes.put("message", responseStatusException.getReason()); exception = (responseStatusException.getCause() != null) ? responseStatusException.getCause() : error; + if (exception instanceof BindingResult bindingResult) { + errorAttributes.put("errors", bindingResult.getAllErrors()); + } } else { exception = error; diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/reactive/error/DefaultErrorAttributesTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/reactive/error/DefaultErrorAttributesTests.java index b375825034..4216503f4a 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/reactive/error/DefaultErrorAttributesTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/reactive/error/DefaultErrorAttributesTests.java @@ -250,6 +250,21 @@ class DefaultErrorAttributesTests { assertThat(attributes).containsEntry("errors", bindingResult.getAllErrors()); } + @Test + void extractBindingResultErrorsThatCausedAResponseStatusException() throws Exception { + Method method = getClass().getDeclaredMethod("method", String.class); + MethodParameter stringParam = new MethodParameter(method, 0); + BindingResult bindingResult = new MapBindingResult(Collections.singletonMap("a", "b"), "objectName"); + bindingResult.addError(new ObjectError("c", "d")); + Exception ex = new WebExchangeBindException(stringParam, bindingResult); + MockServerHttpRequest request = MockServerHttpRequest.get("/test").build(); + Map attributes = this.errorAttributes.getErrorAttributes( + buildServerRequest(request, new ResponseStatusException(HttpStatus.BAD_REQUEST, "Invalid", ex)), + ErrorAttributeOptions.of(Include.MESSAGE, Include.BINDING_ERRORS)); + assertThat(attributes.get("message")).isEqualTo("Invalid"); + assertThat(attributes).containsEntry("errors", bindingResult.getAllErrors()); + } + @Test void extractMethodValidationResultErrors() throws Exception { Object target = "test";