Skip to content

Commit d53c04b

Browse files
committed
Add option to extend exception resolvers
Issue: SPR-13931
1 parent c45ad30 commit d53c04b

File tree

5 files changed

+61
-6
lines changed

5 files changed

+61
-6
lines changed

spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurationSupport.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -713,6 +713,14 @@ protected final List<HttpMessageConverter<?>> getMessageConverters() {
713713
protected void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
714714
}
715715

716+
/**
717+
* Override this method to extend or modify the list of converters after it
718+
* has been configured. This may be useful for example to allow default
719+
* converters to be registered and then insert a custom converter through
720+
* this method.
721+
* @param converters the list of configured converters to extend.
722+
* @since 4.1.3
723+
*/
716724
protected void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
717725
}
718726

@@ -813,6 +821,7 @@ public HandlerExceptionResolver handlerExceptionResolver() {
813821
addDefaultHandlerExceptionResolvers(exceptionResolvers);
814822
}
815823

824+
extendHandlerExceptionResolvers(exceptionResolvers);
816825
HandlerExceptionResolverComposite composite = new HandlerExceptionResolverComposite();
817826
composite.setOrder(0);
818827
composite.setExceptionResolvers(exceptionResolvers);
@@ -831,6 +840,17 @@ public HandlerExceptionResolver handlerExceptionResolver() {
831840
protected void configureHandlerExceptionResolvers(List<HandlerExceptionResolver> exceptionResolvers) {
832841
}
833842

843+
/**
844+
* Override this method to extend or modify the list of
845+
* {@link HandlerExceptionResolver}s after it has been configured. This may
846+
* be useful for example to allow default resolvers to be registered and then
847+
* insert a custom one through this method.
848+
* @param exceptionResolvers the list of configured resolvers to extend.
849+
* @since 4.3.1
850+
*/
851+
protected void extendHandlerExceptionResolvers(List<HandlerExceptionResolver> exceptionResolvers) {
852+
}
853+
834854
/**
835855
* A method available to subclasses for adding default {@link HandlerExceptionResolver}s.
836856
* <p>Adds the following exception resolvers:

spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurer.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -130,6 +130,16 @@ public interface WebMvcConfigurer {
130130
*/
131131
void configureHandlerExceptionResolvers(List<HandlerExceptionResolver> exceptionResolvers);
132132

133+
/**
134+
* A hook for extending or modifying the list of
135+
* {@link HandlerExceptionResolver}s after it has been configured. This may
136+
* be useful for example to allow default resolvers to be registered and then
137+
* insert a custom one through this method.
138+
* @param exceptionResolvers the list of configured resolvers to extend.
139+
* @since 4.3.1
140+
*/
141+
void extendHandlerExceptionResolvers(List<HandlerExceptionResolver> exceptionResolvers);
142+
133143
/**
134144
* Add Spring MVC lifecycle interceptors for pre- and post-processing of
135145
* controller method invocations. Interceptors can be registered to apply

spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurerAdapter.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,14 @@ public void addReturnValueHandlers(List<HandlerMethodReturnValueHandler> returnV
116116
public void configureHandlerExceptionResolvers(List<HandlerExceptionResolver> exceptionResolvers) {
117117
}
118118

119+
/**
120+
* {@inheritDoc}
121+
* <p>This implementation is empty.
122+
*/
123+
@Override
124+
public void extendHandlerExceptionResolvers(List<HandlerExceptionResolver> exceptionResolvers) {
125+
}
126+
119127
/**
120128
* {@inheritDoc}
121129
* <p>This implementation is empty.

spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurerComposite.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -28,7 +28,7 @@
2828
import org.springframework.web.servlet.HandlerExceptionResolver;
2929

3030
/**
31-
* An {@link WebMvcConfigurer} implementation that delegates to other {@link WebMvcConfigurer} instances.
31+
* A {@link WebMvcConfigurer} that delegates to one or more others.
3232
*
3333
* @author Rossen Stoyanchev
3434
* @since 3.1
@@ -106,6 +106,13 @@ public void configureHandlerExceptionResolvers(List<HandlerExceptionResolver> ex
106106
}
107107
}
108108

109+
@Override
110+
public void extendHandlerExceptionResolvers(List<HandlerExceptionResolver> exceptionResolvers) {
111+
for (WebMvcConfigurer delegate : this.delegates) {
112+
delegate.configureHandlerExceptionResolvers(exceptionResolvers);
113+
}
114+
}
115+
109116
@Override
110117
public void addInterceptors(InterceptorRegistry registry) {
111118
for (WebMvcConfigurer delegate : this.delegates) {

spring-webmvc/src/test/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurationSupportExtensionTests.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -70,6 +70,7 @@
7070
import org.springframework.web.servlet.handler.HandlerExceptionResolverComposite;
7171
import org.springframework.web.servlet.handler.SimpleMappingExceptionResolver;
7272
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
73+
import org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver;
7374
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;
7475
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
7576
import org.springframework.web.servlet.resource.ResourceUrlProviderExposingInterceptor;
@@ -241,8 +242,12 @@ public void contentNegotiation() throws Exception {
241242

242243
@Test
243244
public void exceptionResolvers() throws Exception {
244-
HandlerExceptionResolver exceptionResolver = this.config.handlerExceptionResolver();
245-
assertEquals(1, ((HandlerExceptionResolverComposite) exceptionResolver).getExceptionResolvers().size());
245+
List<HandlerExceptionResolver> resolvers = ((HandlerExceptionResolverComposite)
246+
this.config.handlerExceptionResolver()).getExceptionResolvers();
247+
248+
assertEquals(2, resolvers.size());
249+
assertEquals(ResponseStatusExceptionResolver.class, resolvers.get(0).getClass());
250+
assertEquals(SimpleMappingExceptionResolver.class, resolvers.get(1).getClass());
246251
}
247252

248253
@SuppressWarnings("unchecked")
@@ -358,6 +363,11 @@ public void configureHandlerExceptionResolvers(List<HandlerExceptionResolver> ex
358363
exceptionResolvers.add(new SimpleMappingExceptionResolver());
359364
}
360365

366+
@Override
367+
public void extendHandlerExceptionResolvers(List<HandlerExceptionResolver> exceptionResolvers) {
368+
exceptionResolvers.add(0, new ResponseStatusExceptionResolver());
369+
}
370+
361371
@Override
362372
public void configurePathMatch(PathMatchConfigurer configurer) {
363373
configurer.setPathMatcher(new TestPathMatcher());

0 commit comments

Comments
 (0)