Interface WebMvcConfigurer
@EnableWebMvc
.
@EnableWebMvc
-annotated configuration classes may implement
this interface to be called back and given a chance to customize the
default configuration.
- Since:
- 3.1
- Author:
- Rossen Stoyanchev, Keith Donald, David Syer
-
Method Summary
Modifier and TypeMethodDescriptiondefault void
addArgumentResolvers
(List<HandlerMethodArgumentResolver> resolvers) Add resolvers to support custom controller method argument types.default void
addCorsMappings
(CorsRegistry registry) Configure "global" cross-origin request processing.default void
addErrorResponseInterceptors
(List<ErrorResponse.Interceptor> interceptors) Add to the list ofErrorResponse.Interceptor
's to apply when rendering an RFC 7807ProblemDetail
error response.default void
addFormatters
(FormatterRegistry registry) AddConverters
andFormatters
in addition to the ones registered by default.default void
addInterceptors
(InterceptorRegistry registry) Add Spring MVC lifecycle interceptors for pre- and post-processing of controller method invocations and resource handler requests.default void
addResourceHandlers
(ResourceHandlerRegistry registry) Add handlers to serve static resources such as images, js, and, css files from specific locations under web application root, the classpath, and others.default void
Add handlers to support custom controller method return value types.default void
addViewControllers
(ViewControllerRegistry registry) Configure simple automated controllers pre-configured with the response status code and/or a view to render the response body.default void
configureAsyncSupport
(AsyncSupportConfigurer configurer) Configure asynchronous request handling options.default void
Configure content negotiation options.default void
Configure a handler to delegate unhandled requests by forwarding to the Servlet container's "default" servlet.default void
Configure exception resolvers.default void
configureMessageConverters
(List<HttpMessageConverter<?>> converters) Configure theHttpMessageConverter
s for reading from the request body and for writing to the response body.default void
configurePathMatch
(PathMatchConfigurer configurer) Help with configuringHandlerMapping
path matching options such as whether to use parsedPathPatterns
or String pattern matching withPathMatcher
, whether to match trailing slashes, and more.default void
configureViewResolvers
(ViewResolverRegistry registry) Configure view resolvers to translate String-based view names returned from controllers into concreteView
implementations to perform rendering with.default void
Extending or modify the list of exception resolvers configured by default.default void
extendMessageConverters
(List<HttpMessageConverter<?>> converters) Extend or modify the list of converters after it has been, eitherconfigured
or initialized with a default list.default MessageCodesResolver
Provide a customMessageCodesResolver
for building message codes from data binding and validation error codes.default Validator
Provide a customValidator
instead of the one created by default.
-
Method Details
-
configurePathMatch
Help with configuringHandlerMapping
path matching options such as whether to use parsedPathPatterns
or String pattern matching withPathMatcher
, whether to match trailing slashes, and more.- Since:
- 4.0.3
- See Also:
-
configureContentNegotiation
Configure content negotiation options. -
configureAsyncSupport
Configure asynchronous request handling options. -
configureDefaultServletHandling
Configure a handler to delegate unhandled requests by forwarding to the Servlet container's "default" servlet. A common use case for this is when theDispatcherServlet
is mapped to "/" thus overriding the Servlet container's default handling of static resources. -
addFormatters
AddConverters
andFormatters
in addition to the ones registered by default. -
addInterceptors
Add Spring MVC lifecycle interceptors for pre- and post-processing of controller method invocations and resource handler requests. Interceptors can be registered to apply to all requests or be limited to a subset of URL patterns. -
addResourceHandlers
Add handlers to serve static resources such as images, js, and, css files from specific locations under web application root, the classpath, and others.- See Also:
-
addCorsMappings
Configure "global" cross-origin request processing. The configured CORS mappings apply to annotated controllers, functional endpoints, and static resources.Annotated controllers can further declare more fine-grained config via
@CrossOrigin
. In such cases "global" CORS configuration declared here iscombined
with local CORS configuration defined on a controller method.- Since:
- 4.2
- See Also:
-
addViewControllers
Configure simple automated controllers pre-configured with the response status code and/or a view to render the response body. This is useful in cases where there is no need for custom controller logic -- e.g. render a home page, perform simple site URL redirects, return a 404 status with HTML content, a 204 with no content, and more.- See Also:
-
configureViewResolvers
Configure view resolvers to translate String-based view names returned from controllers into concreteView
implementations to perform rendering with.- Since:
- 4.1
-
addArgumentResolvers
Add resolvers to support custom controller method argument types.This does not override the built-in support for resolving handler method arguments. To customize the built-in support for argument resolution, configure
RequestMappingHandlerAdapter
directly.- Parameters:
resolvers
- initially an empty list
-
addReturnValueHandlers
Add handlers to support custom controller method return value types.Using this option does not override the built-in support for handling return values. To customize the built-in support for handling return values, configure RequestMappingHandlerAdapter directly.
- Parameters:
handlers
- initially an empty list
-
configureMessageConverters
Configure theHttpMessageConverter
s for reading from the request body and for writing to the response body.By default, all built-in converters are configured as long as the corresponding 3rd party libraries such Jackson JSON, JAXB2, and others are present on the classpath.
Note that use of this method turns off default converter registration. However, in a Spring Boot application the
WebMvcAutoConfiguration
adds anyHttpMessageConverter
beans as well as default converters. Hence, in a Boot application use HttpMessageConverters. Alternatively, for any scenario, useextendMessageConverters(java.util.List)
to modify the configured list of message converters.- Parameters:
converters
- initially an empty list of converters
-
extendMessageConverters
Extend or modify the list of converters after it has been, eitherconfigured
or initialized with a default list.Note that the order of converter registration is important. Especially in cases where clients accept
MediaType.ALL
the converters configured earlier will be preferred.- Parameters:
converters
- the list of configured converters to be extended- Since:
- 4.1.3
-
configureHandlerExceptionResolvers
Configure exception resolvers.The given list starts out empty. If it is left empty, the framework configures a default set of resolvers, see
WebMvcConfigurationSupport.addDefaultHandlerExceptionResolvers(List, org.springframework.web.accept.ContentNegotiationManager)
. Or if any exception resolvers are added to the list, then the application effectively takes over and must provide, fully initialized, exception resolvers.Alternatively you can use
extendHandlerExceptionResolvers(List)
which allows you to extend or modify the list of exception resolvers configured by default.- Parameters:
resolvers
- initially an empty list- See Also:
-
extendHandlerExceptionResolvers
Extending or modify the list of exception resolvers configured by default. This can be useful for inserting a custom exception resolver without interfering with default ones.- Parameters:
resolvers
- the list of configured resolvers to extend- Since:
- 4.3
- See Also:
-
addErrorResponseInterceptors
Add to the list ofErrorResponse.Interceptor
's to apply when rendering an RFC 7807ProblemDetail
error response.- Parameters:
interceptors
- the interceptors to use- Since:
- 6.2
-
getValidator
Provide a customValidator
instead of the one created by default. The default implementation, assuming JSR-303 is on the classpath, is:OptionalValidatorFactoryBean
. Leave the return value asnull
to keep the default. -
getMessageCodesResolver
Provide a customMessageCodesResolver
for building message codes from data binding and validation error codes. Leave the return value asnull
to keep the default.
-