@ExceptionHandler support for non-@Controller handlers
Closes gh-22619
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2019 the original author or authors.
|
||||
* Copyright 2002-2020 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -180,8 +180,16 @@ public abstract class AbstractHandlerExceptionResolver implements HandlerExcepti
|
||||
}
|
||||
}
|
||||
}
|
||||
// Else only apply if there are no explicit handler mappings.
|
||||
return (this.mappedHandlers == null && this.mappedHandlerClasses == null);
|
||||
return !hasHandlerMappings();
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether there are any handler mappings registered via
|
||||
* {@link #setMappedHandlers(Set)} or {@link #setMappedHandlerClasses(Class[])}.
|
||||
* @since 5.3
|
||||
*/
|
||||
protected boolean hasHandlerMappings() {
|
||||
return (this.mappedHandlers != null || this.mappedHandlerClasses != null);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2017 the original author or authors.
|
||||
* Copyright 2002-2020 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -48,17 +48,31 @@ public abstract class AbstractHandlerMethodExceptionResolver extends AbstractHan
|
||||
handler = handlerMethod.getBean();
|
||||
return super.shouldApplyTo(request, handler);
|
||||
}
|
||||
else if (hasGlobalExceptionHandlers() && hasHandlerMappings()) {
|
||||
return super.shouldApplyTo(request, handler);
|
||||
}
|
||||
else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether this resolver has global exception handlers, e.g. not declared in
|
||||
* the same class as the {@code HandlerMethod} that raised the exception and
|
||||
* therefore can apply to any handler.
|
||||
* @since 5.3
|
||||
*/
|
||||
protected boolean hasGlobalExceptionHandlers() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nullable
|
||||
protected final ModelAndView doResolveException(
|
||||
HttpServletRequest request, HttpServletResponse response, @Nullable Object handler, Exception ex) {
|
||||
|
||||
return doResolveHandlerMethodException(request, response, (HandlerMethod) handler, ex);
|
||||
HandlerMethod handlerMethod = (handler instanceof HandlerMethod ? (HandlerMethod) handler : null);
|
||||
return doResolveHandlerMethodException(request, response, handlerMethod, ex);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -380,6 +380,10 @@ public class ExceptionHandlerExceptionResolver extends AbstractHandlerMethodExce
|
||||
return handlers;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean hasGlobalExceptionHandlers() {
|
||||
return !this.exceptionHandlerAdviceCache.isEmpty();
|
||||
}
|
||||
|
||||
/**
|
||||
* Find an {@code @ExceptionHandler} method and invoke it to handle the raised exception.
|
||||
|
||||
Reference in New Issue
Block a user