Skip to content

Commit 71fd621

Browse files
committed
Polishing
(cherry picked from commit 96f1a0e)
1 parent 1295f62 commit 71fd621

File tree

5 files changed

+139
-140
lines changed

5 files changed

+139
-140
lines changed

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

Lines changed: 74 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -164,10 +164,10 @@ public class WebMvcConfigurationSupport implements ApplicationContextAware, Serv
164164

165165
private List<Object> interceptors;
166166

167-
private ContentNegotiationManager contentNegotiationManager;
168-
169167
private PathMatchConfigurer pathMatchConfigurer;
170168

169+
private ContentNegotiationManager contentNegotiationManager;
170+
171171
private List<HttpMessageConverter<?>> messageConverters;
172172

173173

@@ -186,6 +186,7 @@ public void setServletContext(ServletContext servletContext) {
186186
this.servletContext = servletContext;
187187
}
188188

189+
189190
/**
190191
* Return a {@link RequestMappingHandlerMapping} ordered at 0 for mapping
191192
* requests to annotated controllers.
@@ -207,11 +208,13 @@ public RequestMappingHandlerMapping requestMappingHandlerMapping() {
207208
if (configurer.isUseTrailingSlashMatch() != null) {
208209
handlerMapping.setUseTrailingSlashMatch(configurer.isUseTrailingSlashMatch());
209210
}
210-
if (configurer.getPathMatcher() != null) {
211-
handlerMapping.setPathMatcher(configurer.getPathMatcher());
211+
UrlPathHelper pathHelper = configurer.getUrlPathHelper();
212+
if (pathHelper != null) {
213+
handlerMapping.setUrlPathHelper(pathHelper);
212214
}
213-
if (configurer.getUrlPathHelper() != null) {
214-
handlerMapping.setUrlPathHelper(configurer.getUrlPathHelper());
215+
PathMatcher pathMatcher = configurer.getPathMatcher();
216+
if (pathMatcher != null) {
217+
handlerMapping.setPathMatcher(pathMatcher);
215218
}
216219

217220
return handlerMapping;
@@ -240,48 +243,6 @@ protected final Object[] getInterceptors() {
240243
protected void addInterceptors(InterceptorRegistry registry) {
241244
}
242245

243-
/**
244-
* Return a {@link ContentNegotiationManager} instance to use to determine
245-
* requested {@linkplain MediaType media types} in a given request.
246-
*/
247-
@Bean
248-
public ContentNegotiationManager mvcContentNegotiationManager() {
249-
if (this.contentNegotiationManager == null) {
250-
ContentNegotiationConfigurer configurer = new ContentNegotiationConfigurer(this.servletContext);
251-
configurer.mediaTypes(getDefaultMediaTypes());
252-
configureContentNegotiation(configurer);
253-
try {
254-
this.contentNegotiationManager = configurer.getContentNegotiationManager();
255-
}
256-
catch (Exception ex) {
257-
throw new BeanInitializationException("Could not create ContentNegotiationManager", ex);
258-
}
259-
}
260-
return this.contentNegotiationManager;
261-
}
262-
263-
protected Map<String, MediaType> getDefaultMediaTypes() {
264-
Map<String, MediaType> map = new HashMap<String, MediaType>();
265-
if (romePresent) {
266-
map.put("atom", MediaType.APPLICATION_ATOM_XML);
267-
map.put("rss", MediaType.valueOf("application/rss+xml"));
268-
}
269-
if (jaxb2Present) {
270-
map.put("xml", MediaType.APPLICATION_XML);
271-
}
272-
if (jackson2Present || jacksonPresent) {
273-
map.put("json", MediaType.APPLICATION_JSON);
274-
}
275-
return map;
276-
}
277-
278-
/**
279-
* Override this method to configure content negotiation.
280-
* @see DefaultServletHandlerConfigurer
281-
*/
282-
protected void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
283-
}
284-
285246
/**
286247
* Callback for building the {@link PathMatchConfigurer}.
287248
* Delegates to {@link #configurePathMatch}.
@@ -312,12 +273,8 @@ public void configurePathMatch(PathMatchConfigurer configurer) {
312273
*/
313274
@Bean
314275
public PathMatcher mvcPathMatcher() {
315-
if (getPathMatchConfigurer().getPathMatcher() != null) {
316-
return getPathMatchConfigurer().getPathMatcher();
317-
}
318-
else {
319-
return new AntPathMatcher();
320-
}
276+
PathMatcher pathMatcher = getPathMatchConfigurer().getPathMatcher();
277+
return (pathMatcher != null ? pathMatcher : new AntPathMatcher());
321278
}
322279

323280
/**
@@ -329,12 +286,50 @@ public PathMatcher mvcPathMatcher() {
329286
*/
330287
@Bean
331288
public UrlPathHelper mvcUrlPathHelper() {
332-
if (getPathMatchConfigurer().getUrlPathHelper() != null) {
333-
return getPathMatchConfigurer().getUrlPathHelper();
289+
UrlPathHelper pathHelper = getPathMatchConfigurer().getUrlPathHelper();
290+
return (pathHelper != null ? pathHelper : new UrlPathHelper());
291+
}
292+
293+
/**
294+
* Return a {@link ContentNegotiationManager} instance to use to determine
295+
* requested {@linkplain MediaType media types} in a given request.
296+
*/
297+
@Bean
298+
public ContentNegotiationManager mvcContentNegotiationManager() {
299+
if (this.contentNegotiationManager == null) {
300+
ContentNegotiationConfigurer configurer = new ContentNegotiationConfigurer(this.servletContext);
301+
configurer.mediaTypes(getDefaultMediaTypes());
302+
configureContentNegotiation(configurer);
303+
try {
304+
this.contentNegotiationManager = configurer.getContentNegotiationManager();
305+
}
306+
catch (Exception ex) {
307+
throw new BeanInitializationException("Could not create ContentNegotiationManager", ex);
308+
}
334309
}
335-
else {
336-
return new UrlPathHelper();
310+
return this.contentNegotiationManager;
311+
}
312+
313+
protected Map<String, MediaType> getDefaultMediaTypes() {
314+
Map<String, MediaType> map = new HashMap<String, MediaType>(4);
315+
if (romePresent) {
316+
map.put("atom", MediaType.APPLICATION_ATOM_XML);
317+
map.put("rss", MediaType.valueOf("application/rss+xml"));
318+
}
319+
if (jaxb2Present) {
320+
map.put("xml", MediaType.APPLICATION_XML);
337321
}
322+
if (jackson2Present || jacksonPresent) {
323+
map.put("json", MediaType.APPLICATION_JSON);
324+
}
325+
return map;
326+
}
327+
328+
/**
329+
* Override this method to configure content negotiation.
330+
* @see DefaultServletHandlerConfigurer
331+
*/
332+
protected void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
338333
}
339334

340335
/**
@@ -450,7 +445,6 @@ public RequestMappingHandlerAdapter requestMappingHandlerAdapter() {
450445

451446
AsyncSupportConfigurer configurer = new AsyncSupportConfigurer();
452447
configureAsyncSupport(configurer);
453-
454448
if (configurer.getTaskExecutor() != null) {
455449
adapter.setTaskExecutor(configurer.getTaskExecutor());
456450
}
@@ -475,6 +469,20 @@ protected ConfigurableWebBindingInitializer getConfigurableWebBindingInitializer
475469
return initializer;
476470
}
477471

472+
/**
473+
* Override this method to provide a custom {@link MessageCodesResolver}.
474+
*/
475+
protected MessageCodesResolver getMessageCodesResolver() {
476+
return null;
477+
}
478+
479+
/**
480+
* Override this method to configure asynchronous request processing options.
481+
* @see AsyncSupportConfigurer
482+
*/
483+
protected void configureAsyncSupport(AsyncSupportConfigurer configurer) {
484+
}
485+
478486
/**
479487
* Return a {@link FormattingConversionService} for use with annotated
480488
* controller methods and the {@code spring:eval} JSP tag.
@@ -487,6 +495,12 @@ public FormattingConversionService mvcConversionService() {
487495
return conversionService;
488496
}
489497

498+
/**
499+
* Override this method to add custom {@link Converter}s and {@link Formatter}s.
500+
*/
501+
protected void addFormatters(FormatterRegistry registry) {
502+
}
503+
490504
/**
491505
* Return a global {@link Validator} instance for example for validating
492506
* {@code @ModelAttribute} and {@code @RequestBody} method arguments.
@@ -511,7 +525,7 @@ public Validator mvcValidator() {
511525
catch (LinkageError ex) {
512526
throw new BeanInitializationException("Could not load default validator class", ex);
513527
}
514-
validator = (Validator) BeanUtils.instantiate(clazz);
528+
validator = (Validator) BeanUtils.instantiateClass(clazz);
515529
}
516530
else {
517531
validator = new NoOpValidator();
@@ -527,13 +541,6 @@ protected Validator getValidator() {
527541
return null;
528542
}
529543

530-
/**
531-
* Override this method to provide a custom {@link MessageCodesResolver}.
532-
*/
533-
protected MessageCodesResolver getMessageCodesResolver() {
534-
return null;
535-
}
536-
537544
/**
538545
* Add custom {@link HandlerMethodArgumentResolver}s to use in addition to
539546
* the ones registered by default.
@@ -625,19 +632,6 @@ else if (jacksonPresent) {
625632
}
626633
}
627634

628-
/**
629-
* Override this method to add custom {@link Converter}s and {@link Formatter}s.
630-
*/
631-
protected void addFormatters(FormatterRegistry registry) {
632-
}
633-
634-
/**
635-
* Override this method to configure asynchronous request processing options.
636-
* @see AsyncSupportConfigurer
637-
*/
638-
public void configureAsyncSupport(AsyncSupportConfigurer configurer) {
639-
}
640-
641635
/**
642636
* Returns a {@link HttpRequestHandlerAdapter} for processing requests
643637
* with {@link HttpRequestHandler}s.
@@ -670,11 +664,9 @@ public SimpleControllerHandlerAdapter simpleControllerHandlerAdapter() {
670664
public HandlerExceptionResolver handlerExceptionResolver() {
671665
List<HandlerExceptionResolver> exceptionResolvers = new ArrayList<HandlerExceptionResolver>();
672666
configureHandlerExceptionResolvers(exceptionResolvers);
673-
674667
if (exceptionResolvers.isEmpty()) {
675668
addDefaultHandlerExceptionResolvers(exceptionResolvers);
676669
}
677-
678670
HandlerExceptionResolverComposite composite = new HandlerExceptionResolverComposite();
679671
composite.setOrder(0);
680672
composite.setExceptionResolvers(exceptionResolvers);

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

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,6 @@ public interface WebMvcConfigurer {
6161
*/
6262
void configureMessageConverters(List<HttpMessageConverter<?>> converters);
6363

64-
/**
65-
* Provide a custom {@link Validator} instead of the one created by default.
66-
* The default implementation, assuming JSR-303 is on the classpath, is:
67-
* {@link org.springframework.validation.beanvalidation.LocalValidatorFactoryBean}.
68-
* Leave the return value as {@code null} to keep the default.
69-
*/
70-
Validator getValidator();
71-
7264
/**
7365
* Configure content negotiation options.
7466
*/
@@ -79,7 +71,6 @@ public interface WebMvcConfigurer {
7971
*/
8072
void configureAsyncSupport(AsyncSupportConfigurer configurer);
8173

82-
8374
/**
8475
* Helps with configuring HandlerMappings path matching options such as trailing slash match,
8576
* suffix registration, path matcher and path helper.
@@ -126,13 +117,6 @@ public interface WebMvcConfigurer {
126117
*/
127118
void addInterceptors(InterceptorRegistry registry);
128119

129-
/**
130-
* Provide a custom {@link MessageCodesResolver} for building message codes
131-
* from data binding and validation error codes. Leave the return value as
132-
* {@code null} to keep the default.
133-
*/
134-
MessageCodesResolver getMessageCodesResolver();
135-
136120
/**
137121
* Add view controllers to create a direct mapping between a URL path and
138122
* view name without the need for a controller in between.
@@ -154,4 +138,19 @@ public interface WebMvcConfigurer {
154138
*/
155139
void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer);
156140

141+
/**
142+
* Provide a custom {@link Validator} instead of the one created by default.
143+
* The default implementation, assuming JSR-303 is on the classpath, is:
144+
* {@link org.springframework.validation.beanvalidation.LocalValidatorFactoryBean}.
145+
* Leave the return value as {@code null} to keep the default.
146+
*/
147+
Validator getValidator();
148+
149+
/**
150+
* Provide a custom {@link MessageCodesResolver} for building message codes
151+
* from data binding and validation error codes. Leave the return value as
152+
* {@code null} to keep the default.
153+
*/
154+
MessageCodesResolver getMessageCodesResolver();
155+
157156
}

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

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828

2929
/**
3030
* An implementation of {@link WebMvcConfigurer} with empty methods allowing
31-
* sub-classes to override only the methods they're interested in.
31+
* subclasses to override only the methods they're interested in.
3232
*
3333
* @author Rossen Stoyanchev
3434
* @since 3.1
@@ -49,14 +49,6 @@ public void addFormatters(FormatterRegistry registry) {
4949
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
5050
}
5151

52-
/**
53-
* {@inheritDoc}
54-
* <p>This implementation returns {@code null}
55-
*/
56-
public Validator getValidator() {
57-
return null;
58-
}
59-
6052
/**
6153
* {@inheritDoc}
6254
* <p>This implementation is empty.
@@ -103,36 +95,44 @@ public void configureHandlerExceptionResolvers(List<HandlerExceptionResolver> ex
10395
* {@inheritDoc}
10496
* <p>This implementation is empty.
10597
*/
106-
public MessageCodesResolver getMessageCodesResolver() {
107-
return null;
98+
public void addInterceptors(InterceptorRegistry registry) {
10899
}
109100

110101
/**
111102
* {@inheritDoc}
112103
* <p>This implementation is empty.
113104
*/
114-
public void addInterceptors(InterceptorRegistry registry) {
105+
public void addViewControllers(ViewControllerRegistry registry) {
115106
}
116107

117108
/**
118109
* {@inheritDoc}
119110
* <p>This implementation is empty.
120111
*/
121-
public void addViewControllers(ViewControllerRegistry registry) {
112+
public void addResourceHandlers(ResourceHandlerRegistry registry) {
122113
}
123114

124115
/**
125116
* {@inheritDoc}
126117
* <p>This implementation is empty.
127118
*/
128-
public void addResourceHandlers(ResourceHandlerRegistry registry) {
119+
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
129120
}
130121

131122
/**
132123
* {@inheritDoc}
133-
* <p>This implementation is empty.
124+
* <p>This implementation returns {@code null}.
134125
*/
135-
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
126+
public Validator getValidator() {
127+
return null;
128+
}
129+
130+
/**
131+
* {@inheritDoc}
132+
* <p>This implementation returns {@code null}.
133+
*/
134+
public MessageCodesResolver getMessageCodesResolver() {
135+
return null;
136136
}
137137

138138
}

0 commit comments

Comments
 (0)