mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-08-29 03:06:45 +08:00
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
This commit is contained in:
parent
3927bd81eb
commit
0fa1d0ef2e
@ -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<HandlerExceptionResolver> 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;
|
||||
|
@ -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) {
|
||||
|
Loading…
Reference in New Issue
Block a user