WebMvcConfigurationSupport类使用方法

WebMvcConfigurationSupport类是SpringMVC提供的扩展类,用于提供拦截器、资源处理器等注册功能

使用方法:

@Configuration
public class WebConfig extends WebMvcConfigurationSupport   {


    @Value("${apiTimeOut:5}")
    private Integer apiTimeOut;

    /**
     * 幂等拦截器
     * @see com.his.common.util.web.ApiIdempotent
     * @param registry
     */
    @Override
    protected void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new ApiIdempotentInterceptor(apiTimeOut));
    }
    @Override
    public void addArgumentResolvers(List argumentResolvers) {
        argumentResolvers.add(new MultiRequestBodyArgumentResolver());
    }
 

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");

        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");

    }
    @Override
    public void configurePathMatch(PathMatchConfigurer configurer) {
        //后缀匹配模式,默认为true,路径完全匹配才行(防止.lzma后缀的接口漏洞)
        configurer.setUseSuffixPatternMatch(false);
    }

    @Override
    public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {

    }

注意事项:

一个容器内只能有一个WebMvcConfigurationSupport的实现类,也就是说不能有多个继承类,否则只有一个生效,会造成未知的错误,如果想在已有实现类的基础上(基础jar包中存在webConfig)还想继续添加拦截器,可以选择继承WebConfig,但是要super.addInterceptors,避免丢失注册


@Configuration
public class InterceptorConfig  extends WebConfig {

    @Override
    protected void addInterceptors(InterceptorRegistry registry) {
        super.addInterceptors(registry);
        registry.addInterceptor(new ThreadCacheClearInterceptor());
    }


}

你可能感兴趣的:(Spring框架应用,spring)