Skip to content

Commit 5f38996

Browse files
committed
Add methods for static resolution of @ExceptionHandler
1 parent 6078c27 commit 5f38996

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

spring-web/src/main/java/org/springframework/web/method/annotation/ExceptionHandlerMethodResolver.java

+10-1
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,16 @@ public boolean hasExceptionMappings() {
114114
* @return a method to handle the exception or {@code null}
115115
*/
116116
public Method resolveMethod(Exception exception) {
117-
Class<? extends Exception> exceptionType = exception.getClass();
117+
return resolveMethodByExceptionType(exception.getClass());
118+
}
119+
120+
/**
121+
* Find a method to handle the given exception type. This can be useful if
122+
* an Exception instance is not available (example for tools).
123+
* @param exceptionType the exception type
124+
* @return a method to handle the exception or {@code null}
125+
*/
126+
public Method resolveMethodByExceptionType(Class<? extends Exception> exceptionType) {
118127
Method method = this.exceptionLookupCache.get(exceptionType);
119128
if (method == null) {
120129
method = getMappedMethod(exceptionType);

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

+19-1
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,23 @@ public void setContentNegotiationManager(ContentNegotiationManager contentNegoti
205205
this.contentNegotiationManager = contentNegotiationManager;
206206
}
207207

208+
/**
209+
* Return the configured {@link ContentNegotiationManager}.
210+
*/
211+
public ContentNegotiationManager getContentNegotiationManager() {
212+
return this.contentNegotiationManager;
213+
}
214+
215+
/**
216+
* Return an unmodifiable Map with the {@link ControllerAdvice @ControllerAdvice}
217+
* beans discovered in the ApplicationContext. The returned map will be empty if
218+
* the method is invoked before the bean has been initialized via
219+
* {@link #afterPropertiesSet()}.
220+
*/
221+
public Map<ControllerAdviceBean, ExceptionHandlerMethodResolver> getExceptionHandlerAdviceCache() {
222+
return Collections.unmodifiableMap(this.exceptionHandlerAdviceCache);
223+
}
224+
208225
@Override
209226
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
210227
this.applicationContext = applicationContext;
@@ -365,7 +382,8 @@ protected ServletInvocableHandlerMethod getExceptionHandlerMethod(HandlerMethod
365382
}
366383
for (Entry<ControllerAdviceBean, ExceptionHandlerMethodResolver> entry : this.exceptionHandlerAdviceCache.entrySet()) {
367384
if(entry.getKey().isApplicableToBeanType(handlerType)) {
368-
Method method = entry.getValue().resolveMethod(exception);
385+
ExceptionHandlerMethodResolver resolver = entry.getValue();
386+
Method method = resolver.resolveMethod(exception);
369387
if (method != null) {
370388
return new ServletInvocableHandlerMethod(entry.getKey().resolveBean(), method);
371389
}

0 commit comments

Comments
 (0)