Skip to content

Commit e9e5fee

Browse files
committed
Consistently resolve exceptions for resources and handler functions
Closes gh-33381
1 parent a044357 commit e9e5fee

File tree

2 files changed

+17
-16
lines changed

2 files changed

+17
-16
lines changed

spring-webmvc/src/main/java/org/springframework/web/servlet/handler/AbstractHandlerMethodExceptionResolver.java

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,36 +22,31 @@
2222
import org.springframework.lang.Nullable;
2323
import org.springframework.web.method.HandlerMethod;
2424
import org.springframework.web.servlet.ModelAndView;
25-
import org.springframework.web.servlet.function.HandlerFunction;
2625

2726
/**
2827
* Abstract base class for
2928
* {@link org.springframework.web.servlet.HandlerExceptionResolver HandlerExceptionResolver}
3029
* implementations that support handling exceptions from handlers of type {@link HandlerMethod}.
3130
*
3231
* @author Rossen Stoyanchev
32+
* @author Juergen Hoeller
3333
* @since 3.1
3434
*/
3535
public abstract class AbstractHandlerMethodExceptionResolver extends AbstractHandlerExceptionResolver {
3636

3737
/**
38-
* Checks if the handler is a {@link HandlerMethod} or a {@link HandlerFunction}
39-
* and then delegates to the base class implementation of {@code #shouldApplyTo(HttpServletRequest, Object)}
40-
* passing the bean of the {@code HandlerMethod}. Otherwise, returns {@code false}.
38+
* Checks if the handler is a {@link HandlerMethod} or the resolver has global exception
39+
* handlers and then delegates to the base class implementation of {@code #shouldApplyTo}
40+
* passing the bean of the {@code HandlerMethod} if necessary. Otherwise, returns {@code false}.
41+
* @see HandlerMethod
42+
* @see #hasGlobalExceptionHandlers()
4143
*/
4244
@Override
4345
protected boolean shouldApplyTo(HttpServletRequest request, @Nullable Object handler) {
44-
if (handler == null) {
45-
return super.shouldApplyTo(request, null);
46+
if (handler instanceof HandlerMethod handlerMethod) {
47+
return super.shouldApplyTo(request, handlerMethod.getBean());
4648
}
47-
else if (handler instanceof HandlerMethod handlerMethod) {
48-
handler = handlerMethod.getBean();
49-
return super.shouldApplyTo(request, handler);
50-
}
51-
else if (handler instanceof HandlerFunction<?> handlerFunction) {
52-
return super.shouldApplyTo(request, handlerFunction);
53-
}
54-
else if (hasGlobalExceptionHandlers() && hasHandlerMappings()) {
49+
else if (handler == null || (hasGlobalExceptionHandlers() && hasHandlerMappings())) {
5550
return super.shouldApplyTo(request, handler);
5651
}
5752
else {

spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/ExceptionHandlerExceptionResolver.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
import org.springframework.web.servlet.HandlerMapping;
6161
import org.springframework.web.servlet.ModelAndView;
6262
import org.springframework.web.servlet.View;
63+
import org.springframework.web.servlet.function.HandlerFunction;
6364
import org.springframework.web.servlet.handler.AbstractHandlerMethodExceptionResolver;
6465
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
6566
import org.springframework.web.servlet.resource.ResourceHttpRequestHandler;
@@ -419,8 +420,13 @@ protected boolean hasGlobalExceptionHandlers() {
419420

420421
@Override
421422
protected boolean shouldApplyTo(HttpServletRequest request, @Nullable Object handler) {
422-
return (handler instanceof ResourceHttpRequestHandler ?
423-
hasGlobalExceptionHandlers() : super.shouldApplyTo(request, handler));
423+
if ((handler instanceof ResourceHttpRequestHandler || handler instanceof HandlerFunction) &&
424+
hasGlobalExceptionHandlers() && !hasHandlerMappings()) {
425+
return true; // apply to ResourceHttpRequestHandler and HandlerFunction by default
426+
}
427+
else {
428+
return super.shouldApplyTo(request, handler);
429+
}
424430
}
425431

426432
/**

0 commit comments

Comments
 (0)