From 0fa1d0ef2e47d569d1f1a50b240eca093d102b50 Mon Sep 17 00:00:00 2001 From: Scott Frederick Date: Fri, 5 Jun 2020 14:35:29 -0500 Subject: [PATCH] Handle bind exceptions in management context This commit updates the logic for handling binding exceptions in the management context when it is separate from the application context. The changes allow the exception details to be visible to DefaultErrorAttributes without causing the servlet container to detect an error condition. Fixes gh-21036 --- .../servlet/CompositeHandlerExceptionResolver.java | 11 ++++------- .../CompositeHandlerExceptionResolverTests.java | 1 - 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/web/servlet/CompositeHandlerExceptionResolver.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/web/servlet/CompositeHandlerExceptionResolver.java index 62dbbd402a..345617df91 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/web/servlet/CompositeHandlerExceptionResolver.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/web/servlet/CompositeHandlerExceptionResolver.java @@ -25,6 +25,7 @@ import javax.servlet.http.HttpServletResponse; import org.springframework.beans.factory.ListableBeanFactory; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.web.servlet.error.DefaultErrorAttributes; import org.springframework.core.annotation.AnnotationAwareOrderComparator; import org.springframework.web.servlet.HandlerExceptionResolver; import org.springframework.web.servlet.ModelAndView; @@ -51,13 +52,8 @@ class CompositeHandlerExceptionResolver implements HandlerExceptionResolver { if (this.resolvers == null) { this.resolvers = extractResolvers(); } - ModelAndView resolved = this.resolvers.stream() - .map((resolver) -> resolver.resolveException(request, response, handler, ex)).filter(Objects::nonNull) - .findFirst().orElse(null); - if (resolved != null && resolved.isEmpty()) { - request.setAttribute("javax.servlet.error.exception", ex); - } - return resolved; + return this.resolvers.stream().map((resolver) -> resolver.resolveException(request, response, handler, ex)) + .filter(Objects::nonNull).findFirst().orElse(null); } private List extractResolvers() { @@ -66,6 +62,7 @@ class CompositeHandlerExceptionResolver implements HandlerExceptionResolver { list.remove(this); AnnotationAwareOrderComparator.sort(list); if (list.isEmpty()) { + list.add(new DefaultErrorAttributes()); list.add(new DefaultHandlerExceptionResolver()); } return list; diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/web/servlet/CompositeHandlerExceptionResolverTests.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/web/servlet/CompositeHandlerExceptionResolverTests.java index 129d7dd462..f78843a96e 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/web/servlet/CompositeHandlerExceptionResolverTests.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/web/servlet/CompositeHandlerExceptionResolverTests.java @@ -67,7 +67,6 @@ class CompositeHandlerExceptionResolverTests { ModelAndView resolved = resolver.resolveException(this.request, this.response, null, exception); assertThat(resolved).isNotNull(); assertThat(resolved.isEmpty()).isTrue(); - assertThat(this.request.getAttribute("javax.servlet.error.exception")).isSameAs(exception); } private void load(Class... configs) {