ExceptionHandlerExceptionResolver allows for Model parameter
Issue: SPR-13546
This commit is contained in:
parent
966f95b9b5
commit
44b76d39a4
|
|
@ -293,6 +293,7 @@ public class ExceptionHandlerExceptionResolver extends AbstractHandlerMethodExce
|
||||||
// Type-based argument resolution
|
// Type-based argument resolution
|
||||||
resolvers.add(new ServletRequestMethodArgumentResolver());
|
resolvers.add(new ServletRequestMethodArgumentResolver());
|
||||||
resolvers.add(new ServletResponseMethodArgumentResolver());
|
resolvers.add(new ServletResponseMethodArgumentResolver());
|
||||||
|
resolvers.add(new ModelMethodProcessor());
|
||||||
|
|
||||||
// Custom arguments
|
// Custom arguments
|
||||||
if (getCustomArgumentResolvers() != null) {
|
if (getCustomArgumentResolvers() != null) {
|
||||||
|
|
|
||||||
|
|
@ -32,6 +32,7 @@ import org.springframework.core.annotation.Order;
|
||||||
import org.springframework.mock.web.test.MockHttpServletRequest;
|
import org.springframework.mock.web.test.MockHttpServletRequest;
|
||||||
import org.springframework.mock.web.test.MockHttpServletResponse;
|
import org.springframework.mock.web.test.MockHttpServletResponse;
|
||||||
import org.springframework.stereotype.Controller;
|
import org.springframework.stereotype.Controller;
|
||||||
|
import org.springframework.ui.Model;
|
||||||
import org.springframework.util.ClassUtils;
|
import org.springframework.util.ClassUtils;
|
||||||
import org.springframework.web.bind.annotation.ControllerAdvice;
|
import org.springframework.web.bind.annotation.ControllerAdvice;
|
||||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||||
|
|
@ -49,6 +50,7 @@ import static org.junit.Assert.*;
|
||||||
*
|
*
|
||||||
* @author Rossen Stoyanchev
|
* @author Rossen Stoyanchev
|
||||||
* @author Arjen Poutsma
|
* @author Arjen Poutsma
|
||||||
|
* @author Kazuki Shimizu
|
||||||
* @since 3.1
|
* @since 3.1
|
||||||
*/
|
*/
|
||||||
public class ExceptionHandlerExceptionResolverTests {
|
public class ExceptionHandlerExceptionResolverTests {
|
||||||
|
|
@ -63,6 +65,7 @@ public class ExceptionHandlerExceptionResolverTests {
|
||||||
|
|
||||||
private MockHttpServletResponse response;
|
private MockHttpServletResponse response;
|
||||||
|
|
||||||
|
|
||||||
@BeforeClass
|
@BeforeClass
|
||||||
public static void setupOnce() {
|
public static void setupOnce() {
|
||||||
ExceptionHandlerExceptionResolver r = new ExceptionHandlerExceptionResolver();
|
ExceptionHandlerExceptionResolver r = new ExceptionHandlerExceptionResolver();
|
||||||
|
|
@ -72,12 +75,13 @@ public class ExceptionHandlerExceptionResolverTests {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setUp() throws Exception {
|
public void setup() throws Exception {
|
||||||
this.resolver = new ExceptionHandlerExceptionResolver();
|
this.resolver = new ExceptionHandlerExceptionResolver();
|
||||||
this.request = new MockHttpServletRequest("GET", "/");
|
this.request = new MockHttpServletRequest("GET", "/");
|
||||||
this.response = new MockHttpServletResponse();
|
this.response = new MockHttpServletResponse();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void nullHandler() {
|
public void nullHandler() {
|
||||||
Object handler = null;
|
Object handler = null;
|
||||||
|
|
@ -171,6 +175,18 @@ public class ExceptionHandlerExceptionResolverTests {
|
||||||
assertEquals("IllegalArgumentException", this.response.getContentAsString());
|
assertEquals("IllegalArgumentException", this.response.getContentAsString());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test // SPR-13546
|
||||||
|
public void resolveExceptionModelAtArgument() throws Exception {
|
||||||
|
IllegalArgumentException ex = new IllegalArgumentException();
|
||||||
|
HandlerMethod handlerMethod = new HandlerMethod(new ModelArgumentController(), "handle");
|
||||||
|
this.resolver.afterPropertiesSet();
|
||||||
|
ModelAndView mav = this.resolver.resolveException(this.request, this.response, handlerMethod, ex);
|
||||||
|
|
||||||
|
assertNotNull(mav);
|
||||||
|
assertEquals(1, mav.getModelMap().size());
|
||||||
|
assertEquals("IllegalArgumentException", mav.getModelMap().get("exceptionClassName"));
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void resolveExceptionGlobalHandler() throws Exception {
|
public void resolveExceptionGlobalHandler() throws Exception {
|
||||||
AnnotationConfigApplicationContext cxt = new AnnotationConfigApplicationContext(MyConfig.class);
|
AnnotationConfigApplicationContext cxt = new AnnotationConfigApplicationContext(MyConfig.class);
|
||||||
|
|
@ -253,6 +269,7 @@ public class ExceptionHandlerExceptionResolverTests {
|
||||||
assertEquals(handlerCount, this.resolver.getReturnValueHandlers().getHandlers().size());
|
assertEquals(handlerCount, this.resolver.getReturnValueHandlers().getHandlers().size());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Controller
|
@Controller
|
||||||
static class ModelAndViewController {
|
static class ModelAndViewController {
|
||||||
|
|
||||||
|
|
@ -264,6 +281,7 @@ public class ExceptionHandlerExceptionResolverTests {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Controller
|
@Controller
|
||||||
static class ResponseWriterController {
|
static class ResponseWriterController {
|
||||||
|
|
||||||
|
|
@ -275,6 +293,7 @@ public class ExceptionHandlerExceptionResolverTests {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Controller
|
@Controller
|
||||||
static class ResponseBodyController {
|
static class ResponseBodyController {
|
||||||
|
|
||||||
|
|
@ -287,16 +306,30 @@ public class ExceptionHandlerExceptionResolverTests {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Controller
|
@Controller
|
||||||
static class IoExceptionController {
|
static class IoExceptionController {
|
||||||
|
|
||||||
public void handle() {}
|
public void handle() {}
|
||||||
|
|
||||||
@ExceptionHandler(value=IOException.class)
|
@ExceptionHandler(value = IOException.class)
|
||||||
public void handleException() {
|
public void handleException() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Controller
|
||||||
|
static class ModelArgumentController {
|
||||||
|
|
||||||
|
public void handle() {}
|
||||||
|
|
||||||
|
@ExceptionHandler
|
||||||
|
public void handleException(Exception ex, Model model) {
|
||||||
|
model.addAttribute("exceptionClassName", ClassUtils.getShortName(ex.getClass()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@ControllerAdvice
|
@ControllerAdvice
|
||||||
@Order(1)
|
@Order(1)
|
||||||
static class TestExceptionResolver {
|
static class TestExceptionResolver {
|
||||||
|
|
@ -314,6 +347,7 @@ public class ExceptionHandlerExceptionResolverTests {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ControllerAdvice
|
@ControllerAdvice
|
||||||
@Order(2)
|
@Order(2)
|
||||||
static class AnotherTestExceptionResolver {
|
static class AnotherTestExceptionResolver {
|
||||||
|
|
@ -325,6 +359,7 @@ public class ExceptionHandlerExceptionResolverTests {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
static class MyConfig {
|
static class MyConfig {
|
||||||
|
|
||||||
|
|
@ -337,6 +372,7 @@ public class ExceptionHandlerExceptionResolverTests {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ControllerAdvice("java.lang")
|
@ControllerAdvice("java.lang")
|
||||||
@Order(1)
|
@Order(1)
|
||||||
static class NotCalledTestExceptionResolver {
|
static class NotCalledTestExceptionResolver {
|
||||||
|
|
@ -348,6 +384,7 @@ public class ExceptionHandlerExceptionResolverTests {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ControllerAdvice("org.springframework.web.servlet.mvc.method.annotation")
|
@ControllerAdvice("org.springframework.web.servlet.mvc.method.annotation")
|
||||||
@Order(2)
|
@Order(2)
|
||||||
static class BasePackageTestExceptionResolver {
|
static class BasePackageTestExceptionResolver {
|
||||||
|
|
@ -359,6 +396,7 @@ public class ExceptionHandlerExceptionResolverTests {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ControllerAdvice
|
@ControllerAdvice
|
||||||
@Order(3)
|
@Order(3)
|
||||||
static class DefaultTestExceptionResolver {
|
static class DefaultTestExceptionResolver {
|
||||||
|
|
@ -370,8 +408,10 @@ public class ExceptionHandlerExceptionResolverTests {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
static class MyControllerAdviceConfig {
|
static class MyControllerAdviceConfig {
|
||||||
|
|
||||||
@Bean public NotCalledTestExceptionResolver notCalledTestExceptionResolver() {
|
@Bean public NotCalledTestExceptionResolver notCalledTestExceptionResolver() {
|
||||||
return new NotCalledTestExceptionResolver();
|
return new NotCalledTestExceptionResolver();
|
||||||
}
|
}
|
||||||
|
|
@ -384,4 +424,5 @@ public class ExceptionHandlerExceptionResolverTests {
|
||||||
return new DefaultTestExceptionResolver();
|
return new DefaultTestExceptionResolver();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue