1
1
/*
2
- * Copyright 2002-2015 the original author or authors.
2
+ * Copyright 2002-2016 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
17
17
package org .springframework .web .servlet .config .annotation ;
18
18
19
19
import java .util .ArrayList ;
20
+ import java .util .Collections ;
20
21
import java .util .HashMap ;
21
22
import java .util .List ;
22
23
import java .util .Locale ;
@@ -229,6 +230,7 @@ public ServletContext getServletContext() {
229
230
return this .servletContext ;
230
231
}
231
232
233
+
232
234
/**
233
235
* Return a {@link RequestMappingHandlerMapping} ordered at 0 for mapping
234
236
* requests to annotated controllers.
@@ -251,18 +253,20 @@ public RequestMappingHandlerMapping requestMappingHandlerMapping() {
251
253
if (configurer .isUseTrailingSlashMatch () != null ) {
252
254
handlerMapping .setUseTrailingSlashMatch (configurer .isUseTrailingSlashMatch ());
253
255
}
254
- if (configurer .getPathMatcher () != null ) {
255
- handlerMapping .setPathMatcher (configurer .getPathMatcher ());
256
+ UrlPathHelper pathHelper = configurer .getUrlPathHelper ();
257
+ if (pathHelper != null ) {
258
+ handlerMapping .setUrlPathHelper (pathHelper );
256
259
}
257
- if (configurer .getUrlPathHelper () != null ) {
258
- handlerMapping .setUrlPathHelper (configurer .getUrlPathHelper ());
260
+ PathMatcher pathMatcher = configurer .getPathMatcher ();
261
+ if (pathMatcher != null ) {
262
+ handlerMapping .setPathMatcher (pathMatcher );
259
263
}
260
264
261
265
return handlerMapping ;
262
266
}
263
267
264
268
/**
265
- * Protected method for plugging in a custom sub-class of
269
+ * Protected method for plugging in a custom subclass of
266
270
* {@link RequestMappingHandlerMapping}.
267
271
*/
268
272
protected RequestMappingHandlerMapping createRequestMappingHandlerMapping () {
@@ -335,7 +339,7 @@ public ContentNegotiationManager mvcContentNegotiationManager() {
335
339
}
336
340
337
341
protected Map <String , MediaType > getDefaultMediaTypes () {
338
- Map <String , MediaType > map = new HashMap <String , MediaType >();
342
+ Map <String , MediaType > map = new HashMap <String , MediaType >(4 );
339
343
if (romePresent ) {
340
344
map .put ("atom" , MediaType .APPLICATION_ATOM_XML );
341
345
map .put ("rss" , MediaType .valueOf ("application/rss+xml" ));
@@ -488,18 +492,14 @@ public RequestMappingHandlerAdapter requestMappingHandlerAdapter() {
488
492
adapter .setCustomReturnValueHandlers (returnValueHandlers );
489
493
490
494
if (jackson2Present ) {
491
- List <RequestBodyAdvice > requestBodyAdvices = new ArrayList <RequestBodyAdvice >();
492
- requestBodyAdvices .add (new JsonViewRequestBodyAdvice ());
493
- adapter .setRequestBodyAdvice (requestBodyAdvices );
494
-
495
- List <ResponseBodyAdvice <?>> responseBodyAdvices = new ArrayList <ResponseBodyAdvice <?>>();
496
- responseBodyAdvices .add (new JsonViewResponseBodyAdvice ());
497
- adapter .setResponseBodyAdvice (responseBodyAdvices );
495
+ adapter .setRequestBodyAdvice (
496
+ Collections .<RequestBodyAdvice >singletonList (new JsonViewRequestBodyAdvice ()));
497
+ adapter .setResponseBodyAdvice (
498
+ Collections .<ResponseBodyAdvice <?>>singletonList (new JsonViewResponseBodyAdvice ()));
498
499
}
499
500
500
501
AsyncSupportConfigurer configurer = new AsyncSupportConfigurer ();
501
502
configureAsyncSupport (configurer );
502
-
503
503
if (configurer .getTaskExecutor () != null ) {
504
504
adapter .setTaskExecutor (configurer .getTaskExecutor ());
505
505
}
@@ -524,6 +524,20 @@ protected ConfigurableWebBindingInitializer getConfigurableWebBindingInitializer
524
524
return initializer ;
525
525
}
526
526
527
+ /**
528
+ * Override this method to provide a custom {@link MessageCodesResolver}.
529
+ */
530
+ protected MessageCodesResolver getMessageCodesResolver () {
531
+ return null ;
532
+ }
533
+
534
+ /**
535
+ * Override this method to configure asynchronous request processing options.
536
+ * @see AsyncSupportConfigurer
537
+ */
538
+ protected void configureAsyncSupport (AsyncSupportConfigurer configurer ) {
539
+ }
540
+
527
541
/**
528
542
* Return a {@link FormattingConversionService} for use with annotated
529
543
* controller methods and the {@code spring:eval} JSP tag.
@@ -536,6 +550,12 @@ public FormattingConversionService mvcConversionService() {
536
550
return conversionService ;
537
551
}
538
552
553
+ /**
554
+ * Override this method to add custom {@link Converter}s and {@link Formatter}s.
555
+ */
556
+ protected void addFormatters (FormatterRegistry registry ) {
557
+ }
558
+
539
559
/**
540
560
* Return a global {@link Validator} instance for example for validating
541
561
* {@code @ModelAttribute} and {@code @RequestBody} method arguments.
@@ -560,7 +580,7 @@ public Validator mvcValidator() {
560
580
catch (LinkageError ex ) {
561
581
throw new BeanInitializationException ("Could not load default validator class" , ex );
562
582
}
563
- validator = (Validator ) BeanUtils .instantiate (clazz );
583
+ validator = (Validator ) BeanUtils .instantiateClass (clazz );
564
584
}
565
585
else {
566
586
validator = new NoOpValidator ();
@@ -569,6 +589,13 @@ public Validator mvcValidator() {
569
589
return validator ;
570
590
}
571
591
592
+ /**
593
+ * Override this method to provide a custom {@link Validator}.
594
+ */
595
+ protected Validator getValidator () {
596
+ return null ;
597
+ }
598
+
572
599
/**
573
600
* Return a global {@link PathMatcher} instance for path matching
574
601
* patterns in {@link HandlerMapping}s.
@@ -595,26 +622,8 @@ public PathMatcher mvcPathMatcher() {
595
622
*/
596
623
@ Bean
597
624
public UrlPathHelper mvcUrlPathHelper () {
598
- if (getPathMatchConfigurer ().getUrlPathHelper () != null ) {
599
- return getPathMatchConfigurer ().getUrlPathHelper ();
600
- }
601
- else {
602
- return new UrlPathHelper ();
603
- }
604
- }
605
-
606
- /**
607
- * Override this method to provide a custom {@link Validator}.
608
- */
609
- protected Validator getValidator () {
610
- return null ;
611
- }
612
-
613
- /**
614
- * Override this method to provide a custom {@link MessageCodesResolver}.
615
- */
616
- protected MessageCodesResolver getMessageCodesResolver () {
617
- return null ;
625
+ UrlPathHelper pathHelper = getPathMatchConfigurer ().getUrlPathHelper ();
626
+ return (pathHelper != null ? pathHelper : new UrlPathHelper ());
618
627
}
619
628
620
629
/**
@@ -679,6 +688,14 @@ protected final List<HttpMessageConverter<?>> getMessageConverters() {
679
688
protected void configureMessageConverters (List <HttpMessageConverter <?>> converters ) {
680
689
}
681
690
691
+ /**
692
+ * Override this method to extend or modify the list of converters after it
693
+ * has been configured. This may be useful for example to allow default
694
+ * converters to be registered and then insert a custom converter through
695
+ * this method.
696
+ * @param converters the list of configured converters to extend.
697
+ * @since 4.1.3
698
+ */
682
699
protected void extendMessageConverters (List <HttpMessageConverter <?>> converters ) {
683
700
}
684
701
@@ -719,19 +736,6 @@ else if (gsonPresent) {
719
736
}
720
737
}
721
738
722
- /**
723
- * Override this method to add custom {@link Converter}s and {@link Formatter}s.
724
- */
725
- protected void addFormatters (FormatterRegistry registry ) {
726
- }
727
-
728
- /**
729
- * Override this method to configure asynchronous request processing options.
730
- * @see AsyncSupportConfigurer
731
- */
732
- public void configureAsyncSupport (AsyncSupportConfigurer configurer ) {
733
- }
734
-
735
739
/**
736
740
* Return an instance of {@link CompositeUriComponentsContributor} for use with
737
741
* {@link org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder}.
@@ -774,11 +778,9 @@ public SimpleControllerHandlerAdapter simpleControllerHandlerAdapter() {
774
778
public HandlerExceptionResolver handlerExceptionResolver () {
775
779
List <HandlerExceptionResolver > exceptionResolvers = new ArrayList <HandlerExceptionResolver >();
776
780
configureHandlerExceptionResolvers (exceptionResolvers );
777
-
778
781
if (exceptionResolvers .isEmpty ()) {
779
782
addDefaultHandlerExceptionResolvers (exceptionResolvers );
780
783
}
781
-
782
784
HandlerExceptionResolverComposite composite = new HandlerExceptionResolverComposite ();
783
785
composite .setOrder (0 );
784
786
composite .setExceptionResolvers (exceptionResolvers );
0 commit comments