From 3be521e825bf4e42ad3a24bcbd8ed2780b845ff5 Mon Sep 17 00:00:00 2001 From: Madhura Bhave Date: Wed, 8 Nov 2017 17:49:43 -0800 Subject: [PATCH] Add DefaultHandlerExceptionResolver to child context Fixes gh-10560 --- .../CompositeHandlerExceptionResolver.java | 4 + ...ompositeHandlerExceptionResolverTests.java | 103 ++++++++++++++++++ 2 files changed, 107 insertions(+) create mode 100644 spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/web/servlet/CompositeHandlerExceptionResolverTests.java diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/web/servlet/CompositeHandlerExceptionResolver.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/web/servlet/CompositeHandlerExceptionResolver.java index 11590308972..be7eed3524c 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/web/servlet/CompositeHandlerExceptionResolver.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/web/servlet/CompositeHandlerExceptionResolver.java @@ -28,6 +28,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.annotation.AnnotationAwareOrderComparator; import org.springframework.web.servlet.HandlerExceptionResolver; import org.springframework.web.servlet.ModelAndView; +import org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver; /** * Composite {@link HandlerExceptionResolver}. @@ -60,6 +61,9 @@ class CompositeHandlerExceptionResolver implements HandlerExceptionResolver { this.beanFactory.getBeansOfType(HandlerExceptionResolver.class).values()); list.remove(this); AnnotationAwareOrderComparator.sort(list); + if (list.isEmpty()) { + list.add(new DefaultHandlerExceptionResolver()); + } return list; } diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/web/servlet/CompositeHandlerExceptionResolverTests.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/web/servlet/CompositeHandlerExceptionResolverTests.java new file mode 100644 index 00000000000..e19b9a77a00 --- /dev/null +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/web/servlet/CompositeHandlerExceptionResolverTests.java @@ -0,0 +1,103 @@ +/* + * Copyright 2012-2017 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.actuate.autoconfigure.web.servlet; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +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.context.annotation.Import; +import org.springframework.mock.web.MockHttpServletRequest; +import org.springframework.mock.web.MockHttpServletResponse; +import org.springframework.web.HttpRequestMethodNotSupportedException; +import org.springframework.web.servlet.DispatcherServlet; +import org.springframework.web.servlet.HandlerExceptionResolver; +import org.springframework.web.servlet.ModelAndView; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Tests for {@link CompositeHandlerExceptionResolver}. + * + * @author Madhura Bhave + */ +public class CompositeHandlerExceptionResolverTests { + + private AnnotationConfigApplicationContext context; + + private MockHttpServletRequest request = new MockHttpServletRequest(); + + private MockHttpServletResponse response = new MockHttpServletResponse(); + + @Test + public void resolverShouldDelegateToOtherResolversInContext() throws Exception { + load(TestConfiguration.class); + CompositeHandlerExceptionResolver resolver = (CompositeHandlerExceptionResolver) this.context.getBean(DispatcherServlet.HANDLER_EXCEPTION_RESOLVER_BEAN_NAME); + ModelAndView resolved = resolver.resolveException(this.request, this.response, null, new HttpRequestMethodNotSupportedException("POST")); + assertThat(resolved.getViewName()).isEqualTo("test-view"); + } + + @Test + public void resolverShouldAddDefaultResolverIfNonePresent() throws Exception { + load(BaseConfiguration.class); + CompositeHandlerExceptionResolver resolver = (CompositeHandlerExceptionResolver) this.context.getBean(DispatcherServlet.HANDLER_EXCEPTION_RESOLVER_BEAN_NAME); + ModelAndView resolved = resolver.resolveException(this.request, this.response, null, new HttpRequestMethodNotSupportedException("POST")); + assertThat(resolved).isNotNull(); + } + + private void load(Class... configs) { + AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); + context.register(configs); + context.refresh(); + this.context = context; + } + + @Configuration + static class BaseConfiguration { + + @Bean(name = DispatcherServlet.HANDLER_EXCEPTION_RESOLVER_BEAN_NAME) + public CompositeHandlerExceptionResolver compositeHandlerExceptionResolver() { + return new CompositeHandlerExceptionResolver(); + } + + } + + @Configuration + @Import(BaseConfiguration.class) + static class TestConfiguration { + + @Bean + public HandlerExceptionResolver testResolver() { + return new TestHandlerExceptionResolver(); + } + + } + + static class TestHandlerExceptionResolver implements HandlerExceptionResolver { + + @Override + public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) { + return new ModelAndView("test-view"); + } + + } + +}