Add support for global @ExceptionHandler methods

Before this change @ExceptionHandler methods could be located in and
apply locally within a controller. The change makes it possible to have
such methods applicable globally regardless of the controller that
raised the exception.

The easiest way to do that is to add them to a class annotated with
`@ExceptionResolver`, a new annotation that is also an `@Component`
annotation (and therefore works with component scanning). It is also
possible to register classes containing `@ExceptionHandler` methods
directly with the ExceptionHandlerExceptionResolver.

When multiple `@ExceptionResolver` classes are detected, or registered
directly, the order in which they're used depends on the the `@Order`
annotation (if present) or on the value of the order field (if the
Ordered interface is implemented).

Issue: SPR-9112
This commit is contained in:
Rossen Stoyanchev
2012-06-11 14:34:44 -04:00
parent ccd2da37ce
commit c846198e46
6 changed files with 334 additions and 74 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2011 the original author or authors.
* Copyright 2002-2012 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.
@@ -30,11 +30,17 @@ import java.util.Arrays;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.util.ClassUtils;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ExceptionResolver;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.method.annotation.ModelMethodProcessor;
@@ -44,7 +50,7 @@ import org.springframework.web.servlet.ModelAndView;
/**
* Test fixture with {@link ExceptionHandlerExceptionResolver}.
*
*
* @author Rossen Stoyanchev
* @author Arjen Poutsma
* @since 3.1
@@ -52,7 +58,7 @@ import org.springframework.web.servlet.ModelAndView;
public class ExceptionHandlerExceptionResolverTests {
private static int RESOLVER_COUNT;
private static int HANDLER_COUNT;
private ExceptionHandlerExceptionResolver resolver;
@@ -63,12 +69,12 @@ public class ExceptionHandlerExceptionResolverTests {
@BeforeClass
public static void setupOnce() {
ExceptionHandlerExceptionResolver resolver = new ExceptionHandlerExceptionResolver();
resolver.afterPropertiesSet();
RESOLVER_COUNT = resolver.getArgumentResolvers().getResolvers().size();
HANDLER_COUNT = resolver.getReturnValueHandlers().getHandlers().size();
ExceptionHandlerExceptionResolver r = new ExceptionHandlerExceptionResolver();
r.afterPropertiesSet();
RESOLVER_COUNT = r.getArgumentResolvers().getResolvers().size();
HANDLER_COUNT = r.getReturnValueHandlers().getHandlers().size();
}
@Before
public void setUp() throws Exception {
this.resolver = new ExceptionHandlerExceptionResolver();
@@ -89,7 +95,7 @@ public class ExceptionHandlerExceptionResolverTests {
HandlerMethodArgumentResolver resolver = new ServletRequestMethodArgumentResolver();
this.resolver.setCustomArgumentResolvers(Arrays.asList(resolver));
this.resolver.afterPropertiesSet();
assertTrue(this.resolver.getArgumentResolvers().getResolvers().contains(resolver));
assertMethodProcessorCount(RESOLVER_COUNT + 1, HANDLER_COUNT);
}
@@ -112,7 +118,7 @@ public class ExceptionHandlerExceptionResolverTests {
assertTrue(this.resolver.getReturnValueHandlers().getHandlers().contains(handler));
assertMethodProcessorCount(RESOLVER_COUNT, HANDLER_COUNT + 1);
}
@Test
public void setReturnValueHandlers() {
HandlerMethodReturnValueHandler handler = new ModelMethodProcessor();
@@ -151,7 +157,7 @@ public class ExceptionHandlerExceptionResolverTests {
HandlerMethod handlerMethod = new HandlerMethod(new ResponseBodyController(), "handle");
this.resolver.afterPropertiesSet();
ModelAndView mav = this.resolver.resolveException(this.request, this.response, handlerMethod, ex);
assertNotNull(mav);
assertTrue(mav.isEmpty());
assertEquals("IllegalArgumentException", this.response.getContentAsString());
@@ -169,7 +175,41 @@ public class ExceptionHandlerExceptionResolverTests {
assertEquals("IllegalArgumentException", this.response.getContentAsString());
}
@Test
public void resolveExceptionGlobalHandler() throws UnsupportedEncodingException, NoSuchMethodException {
AnnotationConfigApplicationContext cxt = new AnnotationConfigApplicationContext(MyConfig.class);
this.resolver.setApplicationContext(cxt);
this.resolver.setGlobalExceptionHandlers(new GlobalExceptionHandler());
this.resolver.afterPropertiesSet();
IllegalStateException ex = new IllegalStateException();
HandlerMethod handlerMethod = new HandlerMethod(new ResponseBodyController(), "handle");
ModelAndView mav = this.resolver.resolveException(this.request, this.response, handlerMethod, ex);
assertNotNull("Exception was not handled", mav);
assertTrue(mav.isEmpty());
assertEquals("IllegalStateException", this.response.getContentAsString());
}
@Test
public void resolveExceptionGlobalHandlerOrdered() throws UnsupportedEncodingException, NoSuchMethodException {
AnnotationConfigApplicationContext cxt = new AnnotationConfigApplicationContext(MyConfig.class);
this.resolver.setApplicationContext(cxt);
GlobalExceptionHandler globalHandler = new GlobalExceptionHandler();
globalHandler.setOrder(2);
this.resolver.setGlobalExceptionHandlers(globalHandler);
this.resolver.afterPropertiesSet();
IllegalStateException ex = new IllegalStateException();
HandlerMethod handlerMethod = new HandlerMethod(new ResponseBodyController(), "handle");
ModelAndView mav = this.resolver.resolveException(this.request, this.response, handlerMethod, ex);
assertNotNull("Exception was not handled", mav);
assertTrue(mav.isEmpty());
assertEquals("@ExceptionResolver: IllegalStateException", this.response.getContentAsString());
}
private void assertMethodProcessorCount(int resolverCount, int handlerCount) {
assertEquals(resolverCount, this.resolver.getArgumentResolvers().getResolvers().size());
assertEquals(handlerCount, this.resolver.getReturnValueHandlers().getHandlers().size());
@@ -185,7 +225,7 @@ public class ExceptionHandlerExceptionResolverTests {
return new ModelAndView("errorView", "detail", ex.getMessage());
}
}
@Controller
static class ResponseWriterController {
@@ -204,7 +244,7 @@ public class ExceptionHandlerExceptionResolverTests {
@ExceptionHandler
@ResponseBody
public String handleException(Exception ex) {
public String handleException(IllegalArgumentException ex) {
return ClassUtils.getShortName(ex.getClass());
}
}
@@ -219,4 +259,42 @@ public class ExceptionHandlerExceptionResolverTests {
}
}
static class GlobalExceptionHandler implements Ordered {
private int order;
public int getOrder() {
return order;
}
public void setOrder(int order) {
this.order = order;
}
@ExceptionHandler
@ResponseBody
public String handleException(IllegalStateException ex) {
return ClassUtils.getShortName(ex.getClass());
}
}
@ExceptionResolver
@Order(1)
static class AnnotatedExceptionResolver {
@ExceptionHandler
@ResponseBody
public String handleException(IllegalStateException ex) {
return "@ExceptionResolver: " + ClassUtils.getShortName(ex.getClass());
}
}
@Configuration
static class MyConfig {
@Bean public AnnotatedExceptionResolver exceptionResolver() {
return new AnnotatedExceptionResolver();
}
}
}