diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurationSupport.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurationSupport.java index 72b2afc1e3..46b25b37be 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurationSupport.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurationSupport.java @@ -713,6 +713,14 @@ public class WebMvcConfigurationSupport implements ApplicationContextAware, Serv protected void configureMessageConverters(List> converters) { } + /** + * Override this method to extend or modify the list of converters after it + * has been configured. This may be useful for example to allow default + * converters to be registered and then insert a custom converter through + * this method. + * @param converters the list of configured converters to extend. + * @since 4.1.3 + */ protected void extendMessageConverters(List> converters) { } @@ -813,6 +821,7 @@ public class WebMvcConfigurationSupport implements ApplicationContextAware, Serv addDefaultHandlerExceptionResolvers(exceptionResolvers); } + extendHandlerExceptionResolvers(exceptionResolvers); HandlerExceptionResolverComposite composite = new HandlerExceptionResolverComposite(); composite.setOrder(0); composite.setExceptionResolvers(exceptionResolvers); @@ -831,6 +840,17 @@ public class WebMvcConfigurationSupport implements ApplicationContextAware, Serv protected void configureHandlerExceptionResolvers(List exceptionResolvers) { } + /** + * Override this method to extend or modify the list of + * {@link HandlerExceptionResolver}s after it has been configured. This may + * be useful for example to allow default resolvers to be registered and then + * insert a custom one through this method. + * @param exceptionResolvers the list of configured resolvers to extend. + * @since 4.3.1 + */ + protected void extendHandlerExceptionResolvers(List exceptionResolvers) { + } + /** * A method available to subclasses for adding default {@link HandlerExceptionResolver}s. *

Adds the following exception resolvers: diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurer.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurer.java index 3e9c509f7e..506dc8808e 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurer.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurer.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2014 the original author or authors. + * Copyright 2002-2016 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. @@ -130,6 +130,16 @@ public interface WebMvcConfigurer { */ void configureHandlerExceptionResolvers(List exceptionResolvers); + /** + * A hook for extending or modifying the list of + * {@link HandlerExceptionResolver}s after it has been configured. This may + * be useful for example to allow default resolvers to be registered and then + * insert a custom one through this method. + * @param exceptionResolvers the list of configured resolvers to extend. + * @since 4.3.1 + */ + void extendHandlerExceptionResolvers(List exceptionResolvers); + /** * Add Spring MVC lifecycle interceptors for pre- and post-processing of * controller method invocations. Interceptors can be registered to apply diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurerAdapter.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurerAdapter.java index 90d4b6aefe..1c9e3e0c7e 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurerAdapter.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurerAdapter.java @@ -116,6 +116,14 @@ public abstract class WebMvcConfigurerAdapter implements WebMvcConfigurer { public void configureHandlerExceptionResolvers(List exceptionResolvers) { } + /** + * {@inheritDoc} + *

This implementation is empty. + */ + @Override + public void extendHandlerExceptionResolvers(List exceptionResolvers) { + } + /** * {@inheritDoc} *

This implementation is empty. diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurerComposite.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurerComposite.java index c88ba0ff3b..1b6d6e53cb 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurerComposite.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurerComposite.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2014 the original author or authors. + * Copyright 2002-2016 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. @@ -28,7 +28,7 @@ import org.springframework.web.method.support.HandlerMethodReturnValueHandler; import org.springframework.web.servlet.HandlerExceptionResolver; /** - * An {@link WebMvcConfigurer} implementation that delegates to other {@link WebMvcConfigurer} instances. + * A {@link WebMvcConfigurer} that delegates to one or more others. * * @author Rossen Stoyanchev * @since 3.1 @@ -106,6 +106,13 @@ class WebMvcConfigurerComposite implements WebMvcConfigurer { } } + @Override + public void extendHandlerExceptionResolvers(List exceptionResolvers) { + for (WebMvcConfigurer delegate : this.delegates) { + delegate.configureHandlerExceptionResolvers(exceptionResolvers); + } + } + @Override public void addInterceptors(InterceptorRegistry registry) { for (WebMvcConfigurer delegate : this.delegates) { diff --git a/spring-webmvc/src/test/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurationSupportExtensionTests.java b/spring-webmvc/src/test/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurationSupportExtensionTests.java index 9a6bc3cdcc..ee6c79ab70 100644 --- a/spring-webmvc/src/test/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurationSupportExtensionTests.java +++ b/spring-webmvc/src/test/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurationSupportExtensionTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2014 the original author or authors. + * Copyright 2002-2016 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. @@ -70,6 +70,7 @@ import org.springframework.web.servlet.handler.ConversionServiceExposingIntercep import org.springframework.web.servlet.handler.HandlerExceptionResolverComposite; import org.springframework.web.servlet.handler.SimpleMappingExceptionResolver; import org.springframework.web.servlet.i18n.LocaleChangeInterceptor; +import org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver; import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter; import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping; import org.springframework.web.servlet.resource.ResourceUrlProviderExposingInterceptor; @@ -241,8 +242,12 @@ public class WebMvcConfigurationSupportExtensionTests { @Test public void exceptionResolvers() throws Exception { - HandlerExceptionResolver exceptionResolver = this.config.handlerExceptionResolver(); - assertEquals(1, ((HandlerExceptionResolverComposite) exceptionResolver).getExceptionResolvers().size()); + List resolvers = ((HandlerExceptionResolverComposite) + this.config.handlerExceptionResolver()).getExceptionResolvers(); + + assertEquals(2, resolvers.size()); + assertEquals(ResponseStatusExceptionResolver.class, resolvers.get(0).getClass()); + assertEquals(SimpleMappingExceptionResolver.class, resolvers.get(1).getClass()); } @SuppressWarnings("unchecked") @@ -358,6 +363,11 @@ public class WebMvcConfigurationSupportExtensionTests { exceptionResolvers.add(new SimpleMappingExceptionResolver()); } + @Override + public void extendHandlerExceptionResolvers(List exceptionResolvers) { + exceptionResolvers.add(0, new ResponseStatusExceptionResolver()); + } + @Override public void configurePathMatch(PathMatchConfigurer configurer) { configurer.setPathMatcher(new TestPathMatcher());