public class WebMvcConfigurationSupport extends java.lang.Object implements ApplicationContextAware, ServletContextAware
@EnableWebMvc
to an
application @Configuration
class. An alternative more
advanced option is to extend directly from this class and override methods as
necessary remembering to add @Configuration
to the
subclass and @Bean
to overridden @Bean
methods.
For more details see the Javadoc of @EnableWebMvc
.
This class registers the following HandlerMapping
s:
RequestMappingHandlerMapping
ordered at 0 for mapping requests to annotated controller methods.
HandlerMapping
ordered at 1 to map URL paths directly to view names.
BeanNameUrlHandlerMapping
ordered at 2 to map URL paths to controller bean names.
HandlerMapping
ordered at Integer.MAX_VALUE-1
to serve static resource requests.
HandlerMapping
ordered at Integer.MAX_VALUE
to forward requests to the default servlet.
Registers these HandlerAdapter
s:
RequestMappingHandlerAdapter
for processing requests with annotated controller methods.
HttpRequestHandlerAdapter
for processing requests with HttpRequestHandler
s.
SimpleControllerHandlerAdapter
for processing requests with interface-based Controller
s.
Registers a HandlerExceptionResolverComposite
with this chain of
exception resolvers:
ExceptionHandlerExceptionResolver
for handling exceptions
through @ExceptionHandler
methods.
ResponseStatusExceptionResolver
for exceptions annotated
with @ResponseStatus
.
DefaultHandlerExceptionResolver
for resolving known Spring
exception types
Registers an AntPathMatcher
and a UrlPathHelper
to be used by:
RequestMappingHandlerMapping
,
HandlerMapping
for ViewControllers
HandlerMapping
for serving resources
PathMatchConfigurer
.
Both the RequestMappingHandlerAdapter
and the
ExceptionHandlerExceptionResolver
are configured with default
instances of the following by default:
ContentNegotiationManager
DefaultFormattingConversionService
OptionalValidatorFactoryBean
if a JSR-303 implementation is available on the classpath
HttpMessageConverter
s depending on the third-party
libraries available on the classpath.
EnableWebMvc
,
WebMvcConfigurer
,
WebMvcConfigurerAdapter
Modifier and Type | Class and Description |
---|---|
private static class |
WebMvcConfigurationSupport.EmptyHandlerMapping |
private static class |
WebMvcConfigurationSupport.NoOpValidator |
Modifier and Type | Field and Description |
---|---|
private ApplicationContext |
applicationContext |
private java.util.List<HandlerMethodArgumentResolver> |
argumentResolvers |
private ContentNegotiationManager |
contentNegotiationManager |
private java.util.Map<java.lang.String,CorsConfiguration> |
corsConfigurations |
private static boolean |
gsonPresent |
private java.util.List<java.lang.Object> |
interceptors |
private static boolean |
jackson2Present |
private static boolean |
jackson2XmlPresent |
private static boolean |
jaxb2Present |
private java.util.List<HttpMessageConverter<?>> |
messageConverters |
private PathMatchConfigurer |
pathMatchConfigurer |
private java.util.List<HandlerMethodReturnValueHandler> |
returnValueHandlers |
private static boolean |
romePresent |
private ServletContext |
servletContext |
Constructor and Description |
---|
WebMvcConfigurationSupport() |
Modifier and Type | Method and Description |
---|---|
protected void |
addArgumentResolvers(java.util.List<HandlerMethodArgumentResolver> argumentResolvers)
Add custom
HandlerMethodArgumentResolver s to use in addition to
the ones registered by default. |
protected void |
addCorsMappings(CorsRegistry registry)
Override this method to configure cross origin requests processing.
|
protected void |
addDefaultHandlerExceptionResolvers(java.util.List<HandlerExceptionResolver> exceptionResolvers)
A method available to subclasses for adding default
HandlerExceptionResolver s. |
protected void |
addDefaultHttpMessageConverters(java.util.List<HttpMessageConverter<?>> messageConverters)
Adds a set of default HttpMessageConverter instances to the given list.
|
protected void |
addFormatters(FormatterRegistry registry)
|
protected void |
addInterceptors(InterceptorRegistry registry)
Override this method to add Spring MVC interceptors for
pre- and post-processing of controller invocation.
|
protected void |
addResourceHandlers(ResourceHandlerRegistry registry)
Override this method to add resource handlers for serving static resources.
|
protected void |
addReturnValueHandlers(java.util.List<HandlerMethodReturnValueHandler> returnValueHandlers)
Add custom
HandlerMethodReturnValueHandler s in addition to the
ones registered by default. |
protected void |
addViewControllers(ViewControllerRegistry registry)
Override this method to add view controllers.
|
BeanNameUrlHandlerMapping |
beanNameHandlerMapping()
Return a
BeanNameUrlHandlerMapping ordered at 2 to map URL
paths to controller bean names. |
protected void |
configureAsyncSupport(AsyncSupportConfigurer configurer)
Override this method to configure asynchronous request processing options.
|
protected void |
configureContentNegotiation(ContentNegotiationConfigurer configurer)
Override this method to configure content negotiation.
|
protected void |
configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer)
Override this method to configure "default" Servlet handling.
|
protected void |
configureHandlerExceptionResolvers(java.util.List<HandlerExceptionResolver> exceptionResolvers)
Override this method to configure the list of
HandlerExceptionResolver s to use. |
protected void |
configureMessageConverters(java.util.List<HttpMessageConverter<?>> converters)
Override this method to add custom
HttpMessageConverter s to use
with the RequestMappingHandlerAdapter and the
ExceptionHandlerExceptionResolver . |
protected void |
configurePathMatch(PathMatchConfigurer configurer)
Override this method to configure path matching options.
|
protected void |
configureViewResolvers(ViewResolverRegistry registry)
Override this method to configure view resolution.
|
protected ExceptionHandlerExceptionResolver |
createExceptionHandlerExceptionResolver()
Protected method for plugging in a custom subclass of
ExceptionHandlerExceptionResolver . |
protected RequestMappingHandlerAdapter |
createRequestMappingHandlerAdapter()
Protected method for plugging in a custom subclass of
RequestMappingHandlerAdapter . |
protected RequestMappingHandlerMapping |
createRequestMappingHandlerMapping()
Protected method for plugging in a custom subclass of
RequestMappingHandlerMapping . |
HandlerMapping |
defaultServletHandlerMapping()
Return a handler mapping ordered at Integer.MAX_VALUE with a mapped
default servlet handler.
|
protected void |
extendHandlerExceptionResolvers(java.util.List<HandlerExceptionResolver> exceptionResolvers)
Override this method to extend or modify the list of
HandlerExceptionResolver s after it has been configured. |
protected void |
extendMessageConverters(java.util.List<HttpMessageConverter<?>> converters)
Override this method to extend or modify the list of converters after it
has been configured.
|
ApplicationContext |
getApplicationContext()
Return the associated Spring
ApplicationContext . |
protected java.util.List<HandlerMethodArgumentResolver> |
getArgumentResolvers()
Provide access to the shared custom argument resolvers used by the
RequestMappingHandlerAdapter and the
ExceptionHandlerExceptionResolver . |
protected ConfigurableWebBindingInitializer |
getConfigurableWebBindingInitializer()
Return the
ConfigurableWebBindingInitializer to use for
initializing all WebDataBinder instances. |
protected java.util.Map<java.lang.String,CorsConfiguration> |
getCorsConfigurations()
Return the registered
CorsConfiguration objects,
keyed by path pattern. |
protected java.util.Map<java.lang.String,MediaType> |
getDefaultMediaTypes() |
protected java.lang.Object[] |
getInterceptors()
Provide access to the shared handler interceptors used to configure
HandlerMapping instances with. |
protected MessageCodesResolver |
getMessageCodesResolver()
Override this method to provide a custom
MessageCodesResolver . |
protected java.util.List<HttpMessageConverter<?>> |
getMessageConverters()
Provides access to the shared
HttpMessageConverter s used by the
RequestMappingHandlerAdapter and the
ExceptionHandlerExceptionResolver . |
protected PathMatchConfigurer |
getPathMatchConfigurer()
Callback for building the
PathMatchConfigurer . |
protected java.util.List<HandlerMethodReturnValueHandler> |
getReturnValueHandlers()
Provide access to the shared return value handlers used by the
RequestMappingHandlerAdapter and the
ExceptionHandlerExceptionResolver . |
ServletContext |
getServletContext()
Return the associated
javax.servlet.ServletContext . |
protected Validator |
getValidator()
Override this method to provide a custom
Validator . |
HandlerExceptionResolver |
handlerExceptionResolver()
Returns a
HandlerExceptionResolverComposite containing a list
of exception resolvers obtained either through
configureHandlerExceptionResolvers(List) or through
addDefaultHandlerExceptionResolvers(List) . |
HttpRequestHandlerAdapter |
httpRequestHandlerAdapter()
Returns a
HttpRequestHandlerAdapter for processing requests
with HttpRequestHandler s. |
ContentNegotiationManager |
mvcContentNegotiationManager()
Return a
ContentNegotiationManager instance to use to determine
requested media types in a given request. |
FormattingConversionService |
mvcConversionService()
Return a
FormattingConversionService for use with annotated
controller methods and the spring:eval JSP tag. |
PathMatcher |
mvcPathMatcher()
Return a global
PathMatcher instance for path matching
patterns in HandlerMapping s. |
ResourceUrlProvider |
mvcResourceUrlProvider()
A
ResourceUrlProvider bean for use with the MVC dispatcher. |
CompositeUriComponentsContributor |
mvcUriComponentsContributor()
Return an instance of
CompositeUriComponentsContributor for use with
MvcUriComponentsBuilder . |
UrlPathHelper |
mvcUrlPathHelper()
Return a global
UrlPathHelper instance for path matching
patterns in HandlerMapping s. |
Validator |
mvcValidator()
Return a global
Validator instance for example for validating
@ModelAttribute and @RequestBody method arguments. |
ViewResolver |
mvcViewResolver()
Register a
ViewResolverComposite that contains a chain of view resolvers
to use for view resolution. |
RequestMappingHandlerAdapter |
requestMappingHandlerAdapter()
Returns a
RequestMappingHandlerAdapter for processing requests
through annotated controller methods. |
RequestMappingHandlerMapping |
requestMappingHandlerMapping()
Return a
RequestMappingHandlerMapping ordered at 0 for mapping
requests to annotated controllers. |
HandlerMapping |
resourceHandlerMapping()
Return a handler mapping ordered at Integer.MAX_VALUE-1 with mapped
resource handlers.
|
void |
setApplicationContext(ApplicationContext applicationContext)
Set the Spring
ApplicationContext , e.g. |
void |
setServletContext(ServletContext servletContext)
Set the
javax.servlet.ServletContext , e.g. |
SimpleControllerHandlerAdapter |
simpleControllerHandlerAdapter()
Returns a
SimpleControllerHandlerAdapter for processing requests
with interface-based controllers. |
HandlerMapping |
viewControllerHandlerMapping()
Return a handler mapping ordered at 1 to map URL paths directly to
view names.
|
private static boolean romePresent
private static final boolean jaxb2Present
private static final boolean jackson2Present
private static final boolean jackson2XmlPresent
private static final boolean gsonPresent
private ApplicationContext applicationContext
private ServletContext servletContext
private java.util.List<java.lang.Object> interceptors
private PathMatchConfigurer pathMatchConfigurer
private ContentNegotiationManager contentNegotiationManager
private java.util.List<HandlerMethodArgumentResolver> argumentResolvers
private java.util.List<HandlerMethodReturnValueHandler> returnValueHandlers
private java.util.List<HttpMessageConverter<?>> messageConverters
private java.util.Map<java.lang.String,CorsConfiguration> corsConfigurations
public void setApplicationContext(ApplicationContext applicationContext)
ApplicationContext
, e.g. for resource loading.setApplicationContext
in interface ApplicationContextAware
applicationContext
- the ApplicationContext object to be used by this objectBeanInitializationException
public ApplicationContext getApplicationContext()
ApplicationContext
.public void setServletContext(ServletContext servletContext)
javax.servlet.ServletContext
, e.g. for resource handling,
looking up file extensions, etc.setServletContext
in interface ServletContextAware
servletContext
- ServletContext object to be used by this objectInitializingBean.afterPropertiesSet()
,
ApplicationContextAware.setApplicationContext(org.springframework.context.ApplicationContext)
public ServletContext getServletContext()
javax.servlet.ServletContext
.@Bean public RequestMappingHandlerMapping requestMappingHandlerMapping()
RequestMappingHandlerMapping
ordered at 0 for mapping
requests to annotated controllers.protected RequestMappingHandlerMapping createRequestMappingHandlerMapping()
RequestMappingHandlerMapping
.protected final java.lang.Object[] getInterceptors()
HandlerMapping
instances with. This method cannot be overridden,
use addInterceptors(InterceptorRegistry)
instead.protected void addInterceptors(InterceptorRegistry registry)
InterceptorRegistry
protected PathMatchConfigurer getPathMatchConfigurer()
PathMatchConfigurer
.
Delegates to configurePathMatch(org.springframework.web.servlet.config.annotation.PathMatchConfigurer)
.protected void configurePathMatch(PathMatchConfigurer configurer)
PathMatchConfigurer
@Bean public PathMatcher mvcPathMatcher()
PathMatcher
instance for path matching
patterns in HandlerMapping
s.
This instance can be configured using the PathMatchConfigurer
in configurePathMatch(PathMatchConfigurer)
.@Bean public UrlPathHelper mvcUrlPathHelper()
UrlPathHelper
instance for path matching
patterns in HandlerMapping
s.
This instance can be configured using the PathMatchConfigurer
in configurePathMatch(PathMatchConfigurer)
.@Bean public ContentNegotiationManager mvcContentNegotiationManager()
ContentNegotiationManager
instance to use to determine
requested media types in a given request.protected java.util.Map<java.lang.String,MediaType> getDefaultMediaTypes()
protected void configureContentNegotiation(ContentNegotiationConfigurer configurer)
DefaultServletHandlerConfigurer
@Bean public HandlerMapping viewControllerHandlerMapping()
addViewControllers(org.springframework.web.servlet.config.annotation.ViewControllerRegistry)
.protected void addViewControllers(ViewControllerRegistry registry)
ViewControllerRegistry
@Bean public BeanNameUrlHandlerMapping beanNameHandlerMapping()
BeanNameUrlHandlerMapping
ordered at 2 to map URL
paths to controller bean names.@Bean public HandlerMapping resourceHandlerMapping()
addResourceHandlers(org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry)
.protected void addResourceHandlers(ResourceHandlerRegistry registry)
ResourceHandlerRegistry
@Bean public ResourceUrlProvider mvcResourceUrlProvider()
ResourceUrlProvider
bean for use with the MVC dispatcher.@Bean public HandlerMapping defaultServletHandlerMapping()
configureDefaultServletHandling(org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer)
.protected void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer)
DefaultServletHandlerConfigurer
@Bean public RequestMappingHandlerAdapter requestMappingHandlerAdapter()
RequestMappingHandlerAdapter
for processing requests
through annotated controller methods. Consider overriding one of these
other more fine-grained methods:
addArgumentResolvers(java.util.List<org.springframework.web.method.support.HandlerMethodArgumentResolver>)
for adding custom argument resolvers.
addReturnValueHandlers(java.util.List<org.springframework.web.method.support.HandlerMethodReturnValueHandler>)
for adding custom return value handlers.
configureMessageConverters(java.util.List<org.springframework.http.converter.HttpMessageConverter<?>>)
for adding custom message converters.
protected RequestMappingHandlerAdapter createRequestMappingHandlerAdapter()
RequestMappingHandlerAdapter
.protected ConfigurableWebBindingInitializer getConfigurableWebBindingInitializer()
ConfigurableWebBindingInitializer
to use for
initializing all WebDataBinder
instances.protected MessageCodesResolver getMessageCodesResolver()
MessageCodesResolver
.protected void configureAsyncSupport(AsyncSupportConfigurer configurer)
AsyncSupportConfigurer
@Bean public FormattingConversionService mvcConversionService()
FormattingConversionService
for use with annotated
controller methods and the spring:eval
JSP tag.
Also see addFormatters(org.springframework.format.FormatterRegistry)
as an alternative to overriding this method.protected void addFormatters(FormatterRegistry registry)
@Bean public Validator mvcValidator()
Validator
instance for example for validating
@ModelAttribute
and @RequestBody
method arguments.
Delegates to getValidator()
first and if that returns null
checks the classpath for the presence of a JSR-303 implementations
before creating a OptionalValidatorFactoryBean
.If a JSR-303
implementation is not available, a no-op Validator
is returned.protected final java.util.List<HandlerMethodArgumentResolver> getArgumentResolvers()
RequestMappingHandlerAdapter
and the
ExceptionHandlerExceptionResolver
. This method cannot be
overridden, use addArgumentResolvers(List)
instead.protected void addArgumentResolvers(java.util.List<HandlerMethodArgumentResolver> argumentResolvers)
HandlerMethodArgumentResolver
s to use in addition to
the ones registered by default.
Custom argument resolvers are invoked before built-in resolvers
except for those that rely on the presence of annotations (e.g.
@RequestParameter
, @PathVariable
, etc.).
The latter can be customized by configuring the
RequestMappingHandlerAdapter
directly.
argumentResolvers
- the list of custom converters;
initially an empty list.protected final java.util.List<HandlerMethodReturnValueHandler> getReturnValueHandlers()
RequestMappingHandlerAdapter
and the
ExceptionHandlerExceptionResolver
. This method cannot be
overridden, use addReturnValueHandlers(List)
instead.protected void addReturnValueHandlers(java.util.List<HandlerMethodReturnValueHandler> returnValueHandlers)
HandlerMethodReturnValueHandler
s in addition to the
ones registered by default.
Custom return value handlers are invoked before built-in ones except
for those that rely on the presence of annotations (e.g.
@ResponseBody
, @ModelAttribute
, etc.).
The latter can be customized by configuring the
RequestMappingHandlerAdapter
directly.
returnValueHandlers
- the list of custom handlers;
initially an empty list.protected final java.util.List<HttpMessageConverter<?>> getMessageConverters()
HttpMessageConverter
s used by the
RequestMappingHandlerAdapter
and the
ExceptionHandlerExceptionResolver
.
This method cannot be overridden.
Use configureMessageConverters(List)
instead.
Also see addDefaultHttpMessageConverters(List)
that can be
used to add default message converters.protected void configureMessageConverters(java.util.List<HttpMessageConverter<?>> converters)
HttpMessageConverter
s to use
with the RequestMappingHandlerAdapter
and the
ExceptionHandlerExceptionResolver
. Adding converters to the
list turns off the default converters that would otherwise be registered
by default. Also see addDefaultHttpMessageConverters(List)
that
can be used to add default message converters.converters
- a list to add message converters to;
initially an empty list.protected void extendMessageConverters(java.util.List<HttpMessageConverter<?>> converters)
converters
- the list of configured converters to extend.protected final void addDefaultHttpMessageConverters(java.util.List<HttpMessageConverter<?>> messageConverters)
configureMessageConverters(List)
.messageConverters
- the list to add the default message converters to@Bean public CompositeUriComponentsContributor mvcUriComponentsContributor()
CompositeUriComponentsContributor
for use with
MvcUriComponentsBuilder
.@Bean public HttpRequestHandlerAdapter httpRequestHandlerAdapter()
HttpRequestHandlerAdapter
for processing requests
with HttpRequestHandler
s.@Bean public SimpleControllerHandlerAdapter simpleControllerHandlerAdapter()
SimpleControllerHandlerAdapter
for processing requests
with interface-based controllers.@Bean public HandlerExceptionResolver handlerExceptionResolver()
HandlerExceptionResolverComposite
containing a list
of exception resolvers obtained either through
configureHandlerExceptionResolvers(List)
or through
addDefaultHandlerExceptionResolvers(List)
.
Note: This method cannot be made final due to CGLib
constraints. Rather than overriding it, consider overriding
configureHandlerExceptionResolvers(List)
, which allows
providing a list of resolvers.
protected void configureHandlerExceptionResolvers(java.util.List<HandlerExceptionResolver> exceptionResolvers)
HandlerExceptionResolver
s to use. Adding resolvers to the list
turns off the default resolvers that would otherwise be registered by
default. Also see addDefaultHandlerExceptionResolvers(List)
that can be used to add the default exception resolvers.exceptionResolvers
- a list to add exception resolvers to;
initially an empty list.protected void extendHandlerExceptionResolvers(java.util.List<HandlerExceptionResolver> exceptionResolvers)
HandlerExceptionResolver
s after it has been configured. This may
be useful for example to allow default resolvers to be registered and then
insert a custom one through this method.exceptionResolvers
- the list of configured resolvers to extend.protected final void addDefaultHandlerExceptionResolvers(java.util.List<HandlerExceptionResolver> exceptionResolvers)
HandlerExceptionResolver
s.
Adds the following exception resolvers:
ExceptionHandlerExceptionResolver
for handling exceptions through @ExceptionHandler
methods.
ResponseStatusExceptionResolver
for exceptions annotated with @ResponseStatus
.
DefaultHandlerExceptionResolver
for resolving known Spring exception types
protected ExceptionHandlerExceptionResolver createExceptionHandlerExceptionResolver()
ExceptionHandlerExceptionResolver
.@Bean public ViewResolver mvcViewResolver()
ViewResolverComposite
that contains a chain of view resolvers
to use for view resolution.
By default this resolver is ordered at 0 unless content negotiation view
resolution is used in which case the order is raised to
Ordered.HIGHEST_PRECEDENCE
.
If no other resolvers are configured,
ViewResolverComposite.resolveViewName(String, Locale)
returns null in order
to allow other potential ViewResolver
beans to resolve views.
protected void configureViewResolvers(ViewResolverRegistry registry)
ViewResolverRegistry
protected final java.util.Map<java.lang.String,CorsConfiguration> getCorsConfigurations()
CorsConfiguration
objects,
keyed by path pattern.protected void addCorsMappings(CorsRegistry registry)
CorsRegistry