Spring MVC : 配置器概念模型 WebMvcConfigurer

概述

对于基于Java的配置方式,使用注解@EnableWebMvc会引入缺省Spring MVC配置。而WebMvcConfigurer则是针对Spring MVC配置的配置器的概念模型接口。被@EnableWebMvc注解的配置类可以实现该接口的方法用于定制Spring MVC配置。如下所示 :

@EnableWebMvc
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    @Override
    public void configureDefaultServletHandling(final DefaultServletHandlerConfigurer configurer) {
       // ...
    }
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
      // ...
    }    
    
    //...
 }

源代码分析

所在包

package org.springframework.web.servlet.config.annotation;

接口方法 – Spring MVC配置定制回调方法

#configurePathMatch

	/**
	 * 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
	 * 
	 * @since 4.0.3
	 */
	default void configurePathMatch(PathMatchConfigurer configurer) {
	}

#configureContentNegotiation

配置内容磋商选项(content negotiation options)。


	/**
	 * Configure content negotiation options.
	 */
	default void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
	}

#configureAsyncSupport

配置异步请求处理选项。

	/**
	 * Configure asynchronous request handling options.
	 */
	default void configureAsyncSupport(AsyncSupportConfigurer configurer) {
	}

#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
	 * the DispatcherServlet is mapped to "/" thus overriding the
	 * Servlet container's default handling of static resources.
	 */
	default void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
	}

#addFormatters

	/**
	 * Add  Converter Converters and Formatter Formatters in addition to the ones
	 * registered by default.
	 */
	default void addFormatters(FormatterRegistry registry) {
	}

#addInterceptors

	/**
	 * Add Spring MVC lifecycle interceptors for pre- and post-processing of
	 * controller method invocations. Interceptors can be registered to apply
	 * to all requests or be limited to a subset of URL patterns.
	 * Note that interceptors registered here only apply to
	 * controllers and not to resource handler requests. To intercept requests for
	 * static resources either declare a
	 * org.springframework.web.servlet.handler.MappedInterceptor MappedInterceptor
	 * bean or switch to advanced configuration mode by extending
	 * org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport
	 * WebMvcConfigurationSupport} and then override resourceHandlerMapping.
	 */
	default void addInterceptors(InterceptorRegistry registry) {
	}

#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.
	 */
	default void addResourceHandlers(ResourceHandlerRegistry registry) {
	}

#addCorsMappings

	/**
	 * Configure cross origin requests processing.
	 * @since 4.2
	 */
	default void addCorsMappings(CorsRegistry registry) {
	}

#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.
	 */
	default void addViewControllers(ViewControllerRegistry registry) {
	}

#configureViewResolvers

	/**
	 * Configure view resolvers to translate String-based view names returned from
	 * controllers into concrete org.springframework.web.servlet.View
	 * implementations to perform rendering with.
	 * @since 4.1
	 */
	default void configureViewResolvers(ViewResolverRegistry registry) {
	}

#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.
	 * @param resolvers initially an empty list 初始为空的一个list
	 */
	default void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
	}

#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.
	 * @param handlers initially an empty list
	 */
	default void addReturnValueHandlers(List<HandlerMethodReturnValueHandler> handlers) {
	}

#configureMessageConverters

配置一组HttpMessageConverters用于读写请求/响应的消息体。参数converters为空。如果函数结束时,converters仍旧为空,则一组缺省的HttpMessageConverters会被使用。如果往converters中添加了HttpMessageConverter,则这些converters会覆盖缺省值。

如果想要添加一个converter又不想影响缺省配置,可以考略实现方法#extendMessageConverters(java.util.List)

	/**
	 * Configure the HttpMessageConverter HttpMessageConverters 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.
	 * @param converters initially an empty list of converters 初始为空的一个list
	 */
	default void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
	}

#extendMessageConverters

扩展或者修改一组已经缺省配置了的消息转换器message 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.
	 * @param converters the list of configured converters to extend.
	 * @since 4.1.3
	 */
	default void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
	}

#configureHandlerExceptionResolvers

配置一组异常解析器HandlerExceptionResolver。传入的参数resolvers是一个空list。如果该方法的返回时resolvers仍然为空,则框架会设置一组缺省的异常解析器,具体可以参考方法WebMvcConfigurationSupport#addDefaultHandlerExceptionResolvers(List)

另外要加到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).
	 * 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.
	 * @param resolvers initially an empty list 初始为空的一个list
	 * @see #extendHandlerExceptionResolvers(List)
	 * @see WebMvcConfigurationSupport#addDefaultHandlerExceptionResolvers(List)
	 */
	default void configureHandlerExceptionResolvers(List<HandlerExceptionResolver> resolvers) {
	}

#extendHandlerExceptionResolvers

扩展或者修改缺省配置的异常解析器list。通过该扩展方法,可以在不影响缺省配置的异常解析器的情况下,插入一个定制的异常解析器。

	/**
	 * 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.
	 * @param resolvers the list of configured resolvers to extend
	 * @since 4.3
	 * @see WebMvcConfigurationSupport#addDefaultHandlerExceptionResolvers(List)
	 */
	default void extendHandlerExceptionResolvers(List<HandlerExceptionResolver> resolvers) {
	}

#getValidator

实现此方法可以提供一个定制的Validator覆盖缺省值。当JSR-303classpath上时,缺省实现是:org.springframework.validation.beanvalidation.OptionalValidatorFactoryBean。此函数实现返回null表示使用缺省值。这里该方法缺省实现返回null也表示使用缺省实现。

	/**
	 * Provide a custom Validator instead of the one created by default.
	 * The default implementation, assuming JSR-303 is on the classpath, is:
	 * org.springframework.validation.beanvalidation.OptionalValidatorFactoryBean.
	 * Leave the return value as null to keep the default.
	 */
	@Nullable
	default Validator getValidator() {
		return null;
	}

#getMessageCodesResolver

实现此方法可以提供一个定制的MessageCodesResolver覆盖缺省值。这里的MessageCodesResolver用于从数据绑定中解析消息代码和验证错误代码。该方法返回null表明使用缺省值。

	/**
	 * Provide a custom MessageCodesResolver for building message codes
	 * from data binding and validation error codes. Leave the return value as
	 *  null to keep the default.
	 */
	@Nullable
	default MessageCodesResolver getMessageCodesResolver() {
		return null;
	}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值