接口 WebMvcConfigurer
- 所有已知实现类:
WebMvcConfigurerAdapter
public interface WebMvcConfigurer
Defines callback methods to customize the Java-based configuration for Spring MVC enabled via@EnableWebMvc
.@EnableWebMvc
-annotated configuration classes may implement this interface to be called back and given a chance to customize the default configuration.- 从以下版本开始:
- 3.1
- 作者:
- Rossen Stoyanchev, Keith Donald, David Syer
方法概要
所有方法 实例方法 默认方法 修饰符和类型 方法 说明 default void
addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers)
Add resolvers to support custom controller method argument types.default void
addCorsMappings(CorsRegistry registry)
Configure cross origin requests processing.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
addReturnValueHandlers(List<HandlerMethodReturnValueHandler> handlers)
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
configureContentNegotiation(ContentNegotiationConfigurer configurer)
Configure content negotiation options.default void
configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer)
Configure a handler to delegate unhandled requests by forwarding to the Servlet container's "default" servlet.default void
configureHandlerExceptionResolvers(List<HandlerExceptionResolver> resolvers)
Configure exception resolvers.default void
configureMessageConverters(List<HttpMessageConverter<?>> converters)
Configure theHttpMessageConverters
to use for reading or writing to the body of the request or response.default void
configurePathMatch(PathMatchConfigurer configurer)
Helps with configuring HandlerMappings path matching options such as trailing slash match, suffix registration, path matcher and path helper.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
extendHandlerExceptionResolvers(List<HandlerExceptionResolver> resolvers)
Extending or modify the list of exception resolvers configured by default.default void
extendMessageConverters(List<HttpMessageConverter<?>> converters)
A hook for extending or modifying the list of converters after it has been configured.default MessageCodesResolver
getMessageCodesResolver()
Provide a customMessageCodesResolver
for building message codes from data binding and validation error codes.default Validator
getValidator()
Provide a customValidator
instead of the one created by default.
方法详细资料
configurePathMatch
default void configurePathMatch(PathMatchConfigurer configurer)
Helps with configuring HandlerMappings path matching options such as trailing slash match, suffix registration, path matcher and path helper. Configured path matcher and path helper instances are shared for:- RequestMappings
- ViewControllerMappings
- ResourcesMappings
- 从以下版本开始:
- 4.0.3
configureContentNegotiation
default void configureContentNegotiation(ContentNegotiationConfigurer configurer)
Configure content negotiation options.
configureAsyncSupport
default void configureAsyncSupport(AsyncSupportConfigurer configurer)
Configure asynchronous request handling options.
configureDefaultServletHandling
default void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer)
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
default void addFormatters(FormatterRegistry registry)
AddConverters
andFormatters
in addition to the ones registered by default.
addInterceptors
default void addInterceptors(InterceptorRegistry registry)
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
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.
addCorsMappings
default void addCorsMappings(CorsRegistry registry)
Configure cross origin requests processing.- 从以下版本开始:
- 4.2
addViewControllers
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. 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.
configureViewResolvers
default void configureViewResolvers(ViewResolverRegistry registry)
Configure view resolvers to translate String-based view names returned from controllers into concreteView
implementations to perform rendering with.- 从以下版本开始:
- 4.1
addArgumentResolvers
default void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers)
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.- 参数:
resolvers
- initially an empty list
addReturnValueHandlers
default void addReturnValueHandlers(List<HandlerMethodReturnValueHandler> handlers)
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.
- 参数:
handlers
- initially an empty list
configureMessageConverters
default void configureMessageConverters(List<HttpMessageConverter<?>> converters)
Configure theHttpMessageConverters
to use for reading or writing to the body of the request or response. If no converters are added, a default list of converters is registered.Note that adding converters to the list, turns off default converter registration. To simply add a converter without impacting default registration, consider using the method
extendMessageConverters(java.util.List)
instead.- 参数:
converters
- initially an empty list of converters
extendMessageConverters
default void extendMessageConverters(List<HttpMessageConverter<?>> converters)
A hook for extending or modifying the list of converters after it has been configured. This may be useful for example to allow default converters to be registered and then insert a custom converter through this method.- 参数:
converters
- the list of configured converters to extend.- 从以下版本开始:
- 4.1.3
configureHandlerExceptionResolvers
default void configureHandlerExceptionResolvers(List<HandlerExceptionResolver> resolvers)
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.- 参数:
resolvers
- initially an empty list- 另请参阅:
extendHandlerExceptionResolvers(List)
,WebMvcConfigurationSupport.addDefaultHandlerExceptionResolvers(List, org.springframework.web.accept.ContentNegotiationManager)
extendHandlerExceptionResolvers
default void extendHandlerExceptionResolvers(List<HandlerExceptionResolver> resolvers)
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.- 参数:
resolvers
- the list of configured resolvers to extend- 从以下版本开始:
- 4.3
- 另请参阅:
WebMvcConfigurationSupport.addDefaultHandlerExceptionResolvers(List, org.springframework.web.accept.ContentNegotiationManager)
getValidator
@Nullable default Validator 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
@Nullable default MessageCodesResolver getMessageCodesResolver()
Provide a customMessageCodesResolver
for building message codes from data binding and validation error codes. Leave the return value asnull
to keep the default.