Skip to content

Commit 8d7db8e

Browse files
committed
DelegatingWebMvcConfiguration properly delegates extendHandlerExceptionResolvers
Also fixes the declared visibility of configurePathMatch and configureAsyncSupport. Issue: SPR-14599 (cherry picked from commit d2e3a1a)
1 parent da56758 commit 8d7db8e

File tree

6 files changed

+218
-217
lines changed

6 files changed

+218
-217
lines changed

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

Lines changed: 33 additions & 28 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,16 +46,15 @@ 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

5555
@Override
56-
protected void addInterceptors(InterceptorRegistry registry) {
57-
this.configurers.addInterceptors(registry);
56+
protected void configurePathMatch(PathMatchConfigurer configurer) {
57+
this.configurers.configurePathMatch(configurer);
5858
}
5959

6060
@Override
@@ -63,23 +63,23 @@ protected void configureContentNegotiation(ContentNegotiationConfigurer configur
6363
}
6464

6565
@Override
66-
public void configureAsyncSupport(AsyncSupportConfigurer configurer) {
66+
protected void configureAsyncSupport(AsyncSupportConfigurer configurer) {
6767
this.configurers.configureAsyncSupport(configurer);
6868
}
6969

7070
@Override
71-
public void configurePathMatch(PathMatchConfigurer configurer) {
72-
this.configurers.configurePathMatch(configurer);
71+
protected void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
72+
this.configurers.configureDefaultServletHandling(configurer);
7373
}
7474

7575
@Override
76-
protected void addViewControllers(ViewControllerRegistry registry) {
77-
this.configurers.addViewControllers(registry);
76+
protected void addFormatters(FormatterRegistry registry) {
77+
this.configurers.addFormatters(registry);
7878
}
7979

8080
@Override
81-
protected void configureViewResolvers(ViewResolverRegistry registry) {
82-
this.configurers.configureViewResolvers(registry);
81+
protected void addInterceptors(InterceptorRegistry registry) {
82+
this.configurers.addInterceptors(registry);
8383
}
8484

8585
@Override
@@ -88,8 +88,18 @@ protected void addResourceHandlers(ResourceHandlerRegistry registry) {
8888
}
8989

9090
@Override
91-
protected void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
92-
this.configurers.configureDefaultServletHandling(configurer);
91+
protected void addCorsMappings(CorsRegistry registry) {
92+
this.configurers.addCorsMappings(registry);
93+
}
94+
95+
@Override
96+
protected void addViewControllers(ViewControllerRegistry registry) {
97+
this.configurers.addViewControllers(registry);
98+
}
99+
100+
@Override
101+
protected void configureViewResolvers(ViewResolverRegistry registry) {
102+
this.configurers.configureViewResolvers(registry);
93103
}
94104

95105
@Override
@@ -113,8 +123,13 @@ protected void extendMessageConverters(List<HttpMessageConverter<?>> converters)
113123
}
114124

115125
@Override
116-
protected void addFormatters(FormatterRegistry registry) {
117-
this.configurers.addFormatters(registry);
126+
protected void configureHandlerExceptionResolvers(List<HandlerExceptionResolver> exceptionResolvers) {
127+
this.configurers.configureHandlerExceptionResolvers(exceptionResolvers);
128+
}
129+
130+
@Override
131+
protected void extendHandlerExceptionResolvers(List<HandlerExceptionResolver> exceptionResolvers) {
132+
this.configurers.extendHandlerExceptionResolvers(exceptionResolvers);
118133
}
119134

120135
@Override
@@ -127,14 +142,4 @@ protected MessageCodesResolver getMessageCodesResolver() {
127142
return this.configurers.getMessageCodesResolver();
128143
}
129144

130-
@Override
131-
protected void configureHandlerExceptionResolvers(List<HandlerExceptionResolver> exceptionResolvers) {
132-
this.configurers.configureHandlerExceptionResolvers(exceptionResolvers);
133-
}
134-
135-
@Override
136-
protected void addCorsMappings(CorsRegistry registry) {
137-
this.configurers.addCorsMappings(registry);
138-
}
139-
140145
}

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

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ protected PathMatchConfigurer getPathMatchConfigurer() {
315315
* @see PathMatchConfigurer
316316
* @since 4.0.3
317317
*/
318-
public void configurePathMatch(PathMatchConfigurer configurer) {
318+
protected void configurePathMatch(PathMatchConfigurer configurer) {
319319
}
320320

321321
/**
@@ -531,6 +531,13 @@ protected ConfigurableWebBindingInitializer getConfigurableWebBindingInitializer
531531
return initializer;
532532
}
533533

534+
/**
535+
* Override this method to configure asynchronous request processing options.
536+
* @see AsyncSupportConfigurer
537+
*/
538+
protected void configureAsyncSupport(AsyncSupportConfigurer configurer) {
539+
}
540+
534541
/**
535542
* Return a {@link FormattingConversionService} for use with annotated
536543
* controller methods and the {@code spring:eval} JSP tag.
@@ -543,6 +550,12 @@ public FormattingConversionService mvcConversionService() {
543550
return conversionService;
544551
}
545552

553+
/**
554+
* Override this method to add custom {@link Converter}s and {@link Formatter}s.
555+
*/
556+
protected void addFormatters(FormatterRegistry registry) {
557+
}
558+
546559
/**
547560
* Return a global {@link Validator} instance for example for validating
548561
* {@code @ModelAttribute} and {@code @RequestBody} method arguments.
@@ -762,19 +775,6 @@ else if (gsonPresent) {
762775
}
763776
}
764777

765-
/**
766-
* Override this method to add custom {@link Converter}s and {@link Formatter}s.
767-
*/
768-
protected void addFormatters(FormatterRegistry registry) {
769-
}
770-
771-
/**
772-
* Override this method to configure asynchronous request processing options.
773-
* @see AsyncSupportConfigurer
774-
*/
775-
public void configureAsyncSupport(AsyncSupportConfigurer configurer) {
776-
}
777-
778778
/**
779779
* Return an instance of {@link CompositeUriComponentsContributor} for use with
780780
* {@link org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder}.

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

Lines changed: 84 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -46,51 +46,6 @@
4646
*/
4747
public interface WebMvcConfigurer {
4848

49-
/**
50-
* Add {@link Converter}s and {@link Formatter}s in addition to the ones
51-
* registered by default.
52-
*/
53-
void addFormatters(FormatterRegistry registry);
54-
55-
/**
56-
* Configure the {@link HttpMessageConverter}s to use for reading or writing
57-
* to the body of the request or response. If no converters are added, a
58-
* default list of converters is registered.
59-
* <p><strong>Note</strong> that adding converters to the list, turns off
60-
* default converter registration. To simply add a converter without impacting
61-
* default registration, consider using the method
62-
* {@link #extendMessageConverters(java.util.List)} instead.
63-
* @param converters initially an empty list of converters
64-
*/
65-
void configureMessageConverters(List<HttpMessageConverter<?>> converters);
66-
67-
/**
68-
* A hook for extending or modifying the list of converters after it has been
69-
* configured. This may be useful for example to allow default converters to
70-
* be registered and then insert a custom converter through this method.
71-
* @param converters the list of configured converters to extend.
72-
* @since 4.1.3
73-
*/
74-
void extendMessageConverters(List<HttpMessageConverter<?>> converters);
75-
76-
/**
77-
* Provide a custom {@link Validator} instead of the one created by default.
78-
* The default implementation, assuming JSR-303 is on the classpath, is:
79-
* {@link org.springframework.validation.beanvalidation.OptionalValidatorFactoryBean}.
80-
* Leave the return value as {@code null} to keep the default.
81-
*/
82-
Validator getValidator();
83-
84-
/**
85-
* Configure content negotiation options.
86-
*/
87-
void configureContentNegotiation(ContentNegotiationConfigurer configurer);
88-
89-
/**
90-
* Configure asynchronous request handling options.
91-
*/
92-
void configureAsyncSupport(AsyncSupportConfigurer configurer);
93-
9449
/**
9550
* Helps with configuring HandlerMappings path matching options such as trailing slash match,
9651
* suffix registration, path matcher and path helper.
@@ -105,40 +60,28 @@ public interface WebMvcConfigurer {
10560
void configurePathMatch(PathMatchConfigurer configurer);
10661

10762
/**
108-
* Add resolvers to support custom controller method argument types.
109-
* <p>This does not override the built-in support for resolving handler
110-
* method arguments. To customize the built-in support for argument
111-
* resolution, configure {@link RequestMappingHandlerAdapter} directly.
112-
* @param argumentResolvers initially an empty list
63+
* Configure content negotiation options.
11364
*/
114-
void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers);
65+
void configureContentNegotiation(ContentNegotiationConfigurer configurer);
11566

11667
/**
117-
* Add handlers to support custom controller method return value types.
118-
* <p>Using this option does not override the built-in support for handling
119-
* return values. To customize the built-in support for handling return
120-
* values, configure RequestMappingHandlerAdapter directly.
121-
* @param returnValueHandlers initially an empty list
68+
* Configure asynchronous request handling options.
12269
*/
123-
void addReturnValueHandlers(List<HandlerMethodReturnValueHandler> returnValueHandlers);
70+
void configureAsyncSupport(AsyncSupportConfigurer configurer);
12471

12572
/**
126-
* Configure the {@link HandlerExceptionResolver}s to handle unresolved
127-
* controller exceptions. If no resolvers are added to the list, default
128-
* exception resolvers are added instead.
129-
* @param exceptionResolvers initially an empty list
73+
* Configure a handler to delegate unhandled requests by forwarding to the
74+
* Servlet container's "default" servlet. A common use case for this is when
75+
* the {@link DispatcherServlet} is mapped to "/" thus overriding the
76+
* Servlet container's default handling of static resources.
13077
*/
131-
void configureHandlerExceptionResolvers(List<HandlerExceptionResolver> exceptionResolvers);
78+
void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer);
13279

13380
/**
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
81+
* Add {@link Converter}s and {@link Formatter}s in addition to the ones
82+
* registered by default.
14083
*/
141-
void extendHandlerExceptionResolvers(List<HandlerExceptionResolver> exceptionResolvers);
84+
void addFormatters(FormatterRegistry registry);
14285

14386
/**
14487
* Add Spring MVC lifecycle interceptors for pre- and post-processing of
@@ -155,11 +98,17 @@ public interface WebMvcConfigurer {
15598
void addInterceptors(InterceptorRegistry registry);
15699

157100
/**
158-
* Provide a custom {@link MessageCodesResolver} for building message codes
159-
* from data binding and validation error codes. Leave the return value as
160-
* {@code null} to keep the default.
101+
* Add handlers to serve static resources such as images, js, and, css
102+
* files from specific locations under web application root, the classpath,
103+
* and others.
161104
*/
162-
MessageCodesResolver getMessageCodesResolver();
105+
void addResourceHandlers(ResourceHandlerRegistry registry);
106+
107+
/**
108+
* Configure cross origin requests processing.
109+
* @since 4.2
110+
*/
111+
void addCorsMappings(CorsRegistry registry);
163112

164113
/**
165114
* Configure simple automated controllers pre-configured with the response
@@ -178,24 +127,74 @@ public interface WebMvcConfigurer {
178127
void configureViewResolvers(ViewResolverRegistry registry);
179128

180129
/**
181-
* Add handlers to serve static resources such as images, js, and, css
182-
* files from specific locations under web application root, the classpath,
183-
* and others.
130+
* Add resolvers to support custom controller method argument types.
131+
* <p>This does not override the built-in support for resolving handler
132+
* method arguments. To customize the built-in support for argument
133+
* resolution, configure {@link RequestMappingHandlerAdapter} directly.
134+
* @param argumentResolvers initially an empty list
184135
*/
185-
void addResourceHandlers(ResourceHandlerRegistry registry);
136+
void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers);
186137

187138
/**
188-
* Configure a handler to delegate unhandled requests by forwarding to the
189-
* Servlet container's "default" servlet. A common use case for this is when
190-
* the {@link DispatcherServlet} is mapped to "/" thus overriding the
191-
* Servlet container's default handling of static resources.
139+
* Add handlers to support custom controller method return value types.
140+
* <p>Using this option does not override the built-in support for handling
141+
* return values. To customize the built-in support for handling return
142+
* values, configure RequestMappingHandlerAdapter directly.
143+
* @param returnValueHandlers initially an empty list
192144
*/
193-
void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer);
145+
void addReturnValueHandlers(List<HandlerMethodReturnValueHandler> returnValueHandlers);
194146

195147
/**
196-
* Configure cross origin requests processing.
197-
* @since 4.2
148+
* Configure the {@link HttpMessageConverter}s to use for reading or writing
149+
* to the body of the request or response. If no converters are added, a
150+
* default list of converters is registered.
151+
* <p><strong>Note</strong> that adding converters to the list, turns off
152+
* default converter registration. To simply add a converter without impacting
153+
* default registration, consider using the method
154+
* {@link #extendMessageConverters(java.util.List)} instead.
155+
* @param converters initially an empty list of converters
198156
*/
199-
void addCorsMappings(CorsRegistry registry);
157+
void configureMessageConverters(List<HttpMessageConverter<?>> converters);
158+
159+
/**
160+
* A hook for extending or modifying the list of converters after it has been
161+
* configured. This may be useful for example to allow default converters to
162+
* be registered and then insert a custom converter through this method.
163+
* @param converters the list of configured converters to extend.
164+
* @since 4.1.3
165+
*/
166+
void extendMessageConverters(List<HttpMessageConverter<?>> converters);
167+
168+
/**
169+
* Configure the {@link HandlerExceptionResolver}s to handle unresolved
170+
* controller exceptions. If no resolvers are added to the list, default
171+
* exception resolvers are added instead.
172+
* @param exceptionResolvers initially an empty list
173+
*/
174+
void configureHandlerExceptionResolvers(List<HandlerExceptionResolver> exceptionResolvers);
175+
176+
/**
177+
* A hook for extending or modifying the list of {@link HandlerExceptionResolver}s
178+
* after it has been configured. This may be useful for example to allow default
179+
* resolvers to be registered and then insert a custom one through this method.
180+
* @param exceptionResolvers the list of configured resolvers to extend
181+
* @since 4.3
182+
*/
183+
void extendHandlerExceptionResolvers(List<HandlerExceptionResolver> exceptionResolvers);
184+
185+
/**
186+
* Provide a custom {@link Validator} instead of the one created by default.
187+
* The default implementation, assuming JSR-303 is on the classpath, is:
188+
* {@link org.springframework.validation.beanvalidation.OptionalValidatorFactoryBean}.
189+
* Leave the return value as {@code null} to keep the default.
190+
*/
191+
Validator getValidator();
192+
193+
/**
194+
* Provide a custom {@link MessageCodesResolver} for building message codes
195+
* from data binding and validation error codes. Leave the return value as
196+
* {@code null} to keep the default.
197+
*/
198+
MessageCodesResolver getMessageCodesResolver();
200199

201200
}

0 commit comments

Comments
 (0)