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.
1717package org .springframework .web .servlet .config .annotation ;
1818
1919import java .util .ArrayList ;
20+ import java .util .Collections ;
2021import java .util .HashMap ;
2122import java .util .List ;
2223import java .util .Locale ;
@@ -229,6 +230,7 @@ public ServletContext getServletContext() {
229230 return this .servletContext ;
230231 }
231232
233+
232234 /**
233235 * Return a {@link RequestMappingHandlerMapping} ordered at 0 for mapping
234236 * requests to annotated controllers.
@@ -251,18 +253,20 @@ public RequestMappingHandlerMapping requestMappingHandlerMapping() {
251253 if (configurer .isUseTrailingSlashMatch () != null ) {
252254 handlerMapping .setUseTrailingSlashMatch (configurer .isUseTrailingSlashMatch ());
253255 }
254- if (configurer .getPathMatcher () != null ) {
255- handlerMapping .setPathMatcher (configurer .getPathMatcher ());
256+ UrlPathHelper pathHelper = configurer .getUrlPathHelper ();
257+ if (pathHelper != null ) {
258+ handlerMapping .setUrlPathHelper (pathHelper );
256259 }
257- if (configurer .getUrlPathHelper () != null ) {
258- handlerMapping .setUrlPathHelper (configurer .getUrlPathHelper ());
260+ PathMatcher pathMatcher = configurer .getPathMatcher ();
261+ if (pathMatcher != null ) {
262+ handlerMapping .setPathMatcher (pathMatcher );
259263 }
260264
261265 return handlerMapping ;
262266 }
263267
264268 /**
265- * Protected method for plugging in a custom sub-class of
269+ * Protected method for plugging in a custom subclass of
266270 * {@link RequestMappingHandlerMapping}.
267271 */
268272 protected RequestMappingHandlerMapping createRequestMappingHandlerMapping () {
@@ -335,7 +339,7 @@ public ContentNegotiationManager mvcContentNegotiationManager() {
335339 }
336340
337341 protected Map <String , MediaType > getDefaultMediaTypes () {
338- Map <String , MediaType > map = new HashMap <String , MediaType >();
342+ Map <String , MediaType > map = new HashMap <String , MediaType >(4 );
339343 if (romePresent ) {
340344 map .put ("atom" , MediaType .APPLICATION_ATOM_XML );
341345 map .put ("rss" , MediaType .valueOf ("application/rss+xml" ));
@@ -488,18 +492,14 @@ public RequestMappingHandlerAdapter requestMappingHandlerAdapter() {
488492 adapter .setCustomReturnValueHandlers (returnValueHandlers );
489493
490494 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 ()));
498499 }
499500
500501 AsyncSupportConfigurer configurer = new AsyncSupportConfigurer ();
501502 configureAsyncSupport (configurer );
502-
503503 if (configurer .getTaskExecutor () != null ) {
504504 adapter .setTaskExecutor (configurer .getTaskExecutor ());
505505 }
@@ -524,6 +524,20 @@ protected ConfigurableWebBindingInitializer getConfigurableWebBindingInitializer
524524 return initializer ;
525525 }
526526
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+
527541 /**
528542 * Return a {@link FormattingConversionService} for use with annotated
529543 * controller methods and the {@code spring:eval} JSP tag.
@@ -536,6 +550,12 @@ public FormattingConversionService mvcConversionService() {
536550 return conversionService ;
537551 }
538552
553+ /**
554+ * Override this method to add custom {@link Converter}s and {@link Formatter}s.
555+ */
556+ protected void addFormatters (FormatterRegistry registry ) {
557+ }
558+
539559 /**
540560 * Return a global {@link Validator} instance for example for validating
541561 * {@code @ModelAttribute} and {@code @RequestBody} method arguments.
@@ -560,7 +580,7 @@ public Validator mvcValidator() {
560580 catch (LinkageError ex ) {
561581 throw new BeanInitializationException ("Could not load default validator class" , ex );
562582 }
563- validator = (Validator ) BeanUtils .instantiate (clazz );
583+ validator = (Validator ) BeanUtils .instantiateClass (clazz );
564584 }
565585 else {
566586 validator = new NoOpValidator ();
@@ -569,6 +589,13 @@ public Validator mvcValidator() {
569589 return validator ;
570590 }
571591
592+ /**
593+ * Override this method to provide a custom {@link Validator}.
594+ */
595+ protected Validator getValidator () {
596+ return null ;
597+ }
598+
572599 /**
573600 * Return a global {@link PathMatcher} instance for path matching
574601 * patterns in {@link HandlerMapping}s.
@@ -595,26 +622,8 @@ public PathMatcher mvcPathMatcher() {
595622 */
596623 @ Bean
597624 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 ());
618627 }
619628
620629 /**
@@ -679,6 +688,14 @@ protected final List<HttpMessageConverter<?>> getMessageConverters() {
679688 protected void configureMessageConverters (List <HttpMessageConverter <?>> converters ) {
680689 }
681690
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+ */
682699 protected void extendMessageConverters (List <HttpMessageConverter <?>> converters ) {
683700 }
684701
@@ -719,19 +736,6 @@ else if (gsonPresent) {
719736 }
720737 }
721738
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-
735739 /**
736740 * Return an instance of {@link CompositeUriComponentsContributor} for use with
737741 * {@link org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder}.
@@ -774,11 +778,9 @@ public SimpleControllerHandlerAdapter simpleControllerHandlerAdapter() {
774778 public HandlerExceptionResolver handlerExceptionResolver () {
775779 List <HandlerExceptionResolver > exceptionResolvers = new ArrayList <HandlerExceptionResolver >();
776780 configureHandlerExceptionResolvers (exceptionResolvers );
777-
778781 if (exceptionResolvers .isEmpty ()) {
779782 addDefaultHandlerExceptionResolvers (exceptionResolvers );
780783 }
781-
782784 HandlerExceptionResolverComposite composite = new HandlerExceptionResolverComposite ();
783785 composite .setOrder (0 );
784786 composite .setExceptionResolvers (exceptionResolvers );
0 commit comments