Skip to content

Commit d2e3a1a

Browse files
committed
DelegatingWebMvcConfiguration properly delegates extendHandlerExceptionResolvers
Issue: SPR-14599
1 parent 53819c4 commit d2e3a1a

File tree

4 files changed

+25
-22
lines changed

4 files changed

+25
-22
lines changed

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 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.
@@ -22,14 +22,15 @@
2222
import org.springframework.context.annotation.Configuration;
2323
import org.springframework.format.FormatterRegistry;
2424
import org.springframework.http.converter.HttpMessageConverter;
25+
import org.springframework.util.CollectionUtils;
2526
import org.springframework.validation.MessageCodesResolver;
2627
import org.springframework.validation.Validator;
2728
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
2829
import org.springframework.web.method.support.HandlerMethodReturnValueHandler;
2930
import org.springframework.web.servlet.HandlerExceptionResolver;
3031

3132
/**
32-
* A sub-class of {@code WebMvcConfigurationSupport} that detects and delegates
33+
* A subclass of {@code WebMvcConfigurationSupport} that detects and delegates
3334
* to all beans of type {@link WebMvcConfigurer} allowing them to customize the
3435
* configuration provided by {@code WebMvcConfigurationSupport}. This is the
3536
* class actually imported by {@link EnableWebMvc @EnableWebMvc}.
@@ -45,10 +46,9 @@ public class DelegatingWebMvcConfiguration extends WebMvcConfigurationSupport {
4546

4647
@Autowired(required = false)
4748
public void setConfigurers(List<WebMvcConfigurer> configurers) {
48-
if (configurers == null || configurers.isEmpty()) {
49-
return;
49+
if (!CollectionUtils.isEmpty(configurers)) {
50+
this.configurers.addWebMvcConfigurers(configurers);
5051
}
51-
this.configurers.addWebMvcConfigurers(configurers);
5252
}
5353

5454

@@ -132,6 +132,11 @@ protected void configureHandlerExceptionResolvers(List<HandlerExceptionResolver>
132132
this.configurers.configureHandlerExceptionResolvers(exceptionResolvers);
133133
}
134134

135+
@Override
136+
protected void extendHandlerExceptionResolvers(List<HandlerExceptionResolver> exceptionResolvers) {
137+
this.configurers.extendHandlerExceptionResolvers(exceptionResolvers);
138+
}
139+
135140
@Override
136141
protected void addCorsMappings(CorsRegistry registry) {
137142
this.configurers.addCorsMappings(registry);

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -131,11 +131,10 @@ public interface WebMvcConfigurer {
131131
void configureHandlerExceptionResolvers(List<HandlerExceptionResolver> exceptionResolvers);
132132

133133
/**
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.
134+
* A hook for extending or modifying the list of {@link HandlerExceptionResolver}s
135+
* after it has been configured. This may be useful for example to allow default
136+
* resolvers to be registered and then insert a custom one through this method.
137+
* @param exceptionResolvers the list of configured resolvers to extend
139138
* @since 4.3
140139
*/
141140
void extendHandlerExceptionResolvers(List<HandlerExceptionResolver> exceptionResolvers);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ public void configureHandlerExceptionResolvers(List<HandlerExceptionResolver> ex
109109
@Override
110110
public void extendHandlerExceptionResolvers(List<HandlerExceptionResolver> exceptionResolvers) {
111111
for (WebMvcConfigurer delegate : this.delegates) {
112-
delegate.configureHandlerExceptionResolvers(exceptionResolvers);
112+
delegate.extendHandlerExceptionResolvers(exceptionResolvers);
113113
}
114114
}
115115

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

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
package org.springframework.web.servlet.config.annotation;
1818

1919
import java.util.ArrayList;
20-
import java.util.Arrays;
20+
import java.util.Collections;
2121
import java.util.List;
2222

2323
import org.junit.Before;
@@ -89,10 +89,10 @@ public void setUp() {
8989
delegatingConfig = new DelegatingWebMvcConfiguration();
9090
}
9191

92+
9293
@Test
9394
public void requestMappingHandlerAdapter() throws Exception {
94-
95-
delegatingConfig.setConfigurers(Arrays.asList(webMvcConfigurer));
95+
delegatingConfig.setConfigurers(Collections.singletonList(webMvcConfigurer));
9696
RequestMappingHandlerAdapter adapter = delegatingConfig.requestMappingHandlerAdapter();
9797

9898
ConfigurableWebBindingInitializer initializer = (ConfigurableWebBindingInitializer) adapter.getWebBindingInitializer();
@@ -141,7 +141,7 @@ public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
141141
public void getCustomValidator() {
142142
given(webMvcConfigurer.getValidator()).willReturn(new LocalValidatorFactoryBean());
143143

144-
delegatingConfig.setConfigurers(Arrays.asList(webMvcConfigurer));
144+
delegatingConfig.setConfigurers(Collections.singletonList(webMvcConfigurer));
145145
delegatingConfig.mvcValidator();
146146

147147
verify(webMvcConfigurer).getValidator();
@@ -151,16 +151,15 @@ public void getCustomValidator() {
151151
public void getCustomMessageCodesResolver() {
152152
given(webMvcConfigurer.getMessageCodesResolver()).willReturn(new DefaultMessageCodesResolver());
153153

154-
delegatingConfig.setConfigurers(Arrays.asList(webMvcConfigurer));
154+
delegatingConfig.setConfigurers(Collections.singletonList(webMvcConfigurer));
155155
delegatingConfig.getMessageCodesResolver();
156156

157157
verify(webMvcConfigurer).getMessageCodesResolver();
158158
}
159159

160160
@Test
161161
public void handlerExceptionResolver() throws Exception {
162-
163-
delegatingConfig.setConfigurers(Arrays.asList(webMvcConfigurer));
162+
delegatingConfig.setConfigurers(Collections.singletonList(webMvcConfigurer));
164163
delegatingConfig.handlerExceptionResolver();
165164

166165
verify(webMvcConfigurer).configureMessageConverters(converters.capture());
@@ -186,7 +185,7 @@ public void configureHandlerExceptionResolvers(List<HandlerExceptionResolver> ex
186185
delegatingConfig.setConfigurers(configurers);
187186

188187
HandlerExceptionResolverComposite composite =
189-
(HandlerExceptionResolverComposite) delegatingConfig.handlerExceptionResolver();
188+
(HandlerExceptionResolverComposite) delegatingConfig.handlerExceptionResolver();
190189
assertEquals("Only one custom converter is expected", 1, composite.getExceptionResolvers().size());
191190
}
192191

@@ -200,9 +199,9 @@ public void configurePathMatch() throws Exception {
200199
@Override
201200
public void configurePathMatch(PathMatchConfigurer configurer) {
202201
configurer.setUseRegisteredSuffixPatternMatch(true)
203-
.setUseTrailingSlashMatch(false)
204-
.setUrlPathHelper(pathHelper)
205-
.setPathMatcher(pathMatcher);
202+
.setUseTrailingSlashMatch(false)
203+
.setUrlPathHelper(pathHelper)
204+
.setPathMatcher(pathMatcher);
206205
}
207206
});
208207
delegatingConfig.setConfigurers(configurers);

0 commit comments

Comments
 (0)