继承 WebMvcConfigurationSupport类后无法访问Swagger页面问题


项目中为了注册拦截器写了一个配置类继承
WebMvcConfigurationSupport,结果居然无法访问Swagger的接口文档了(localhost:xxxx/doc.html),最后发现是WebMvcConfigurationSupport将静态资源都禁止访问了,需重写一个资源拦截策略

介绍WebMvcConfigurationSupport:

这是提供 MVC Java 配置背后的配置的主类。 它通常通过将@EnableWebMvc添加到应用程序@Configuration类来导入。 另一种更高级的选项是直接从此类扩展并根据需要覆盖方法
记住将@Configuration添加到子类和@Bean以覆盖@Bean方法。 有关更多详细信息,请参阅@EnableWebMvc的 javadoc。
此类注册以下HandlerMappings :

RequestMappingHandlerMapping排序为 0,用于将请求映射到带注释的控制器方法。

HandlerMapping在 1 处排序以将 URL 路径直接映射到视图名称。

BeanNameUrlHandlerMapping在 2 处排序以将 URL 路径映射到控制器 bean 名称。

RouterFunctionMapping在 3 RouterFunctionMapping订购以映射路由器功能。

在WebMvcConfigurationSupport类中addResourceHandlers是一个空方法:

/**
	 * Override this method to add resource handlers for serving static resources.
	 * @see ResourceHandlerRegistry
	 */
	protected void addResourceHandlers(ResourceHandlerRegistry registry) {
    
    
	}

ResourceHandlerRegistry:

存储资源处理程序的注册,用于通过 Spring MVC 提供静态资源,例如图像、css 文件和其他资源,包括设置为在 Web 浏览器中高效加载而优化的缓存标头。 可以从 Web 应用程序根目录下的位置、类路径和其他位置提供资源。
要创建资源处理程序,请使用addResourceHandler(String…)提供应为其调用处理程序以提供静态资源(例如"/resources/…" )的 URL 路径模式。
然后在返回的ResourceHandlerRegistration上使用其他方法来添加一个或多个从中提供静态内容的位置(例如 { “/” , “classpath:/META-INF/public-web-resources/” })或指定缓存服务资源的时间。
addResourceHandler方法:添加资源处理程序以提供静态资源。 为匹配指定 URL 路径模式之一的请求调用处理程序。
支持诸如"/static/**“或”/css/{filename:\w+\.css}"类的模式。
源码:

public ResourceHandlerRegistration addResourceHandler(String... pathPatterns) {
    
    
	ResourceHandlerRegistration registration = new ResourceHandlerRegistration(pathPatterns);
	this.registrations.add(registration);
	return registration;
}

自己项目原代码:


@Configuration
//
@EnableTransactionManagement
public class MyConfig extends WebMvcConfigurationSupport {
    
    


@Autowired
private RepeatInterceptor repeatInterceptor;
@Override
public void addInterceptors(InterceptorRegistry registry)
{
    
    
    registry.addInterceptor(repeatInterceptor);

}
    //super.addInterceptors(registry)
    // .excludePathPatterns("/doc.html")  ;
    //添加重复提交的拦截器


}

修改后:


@Configuration
//
@EnableTransactionManagement
public class MyConfig extends WebMvcConfigurationSupport {
    
    
    //在配置拦截器时,可以继承WebMvcConfigurationSupport,也可以实现WebMvcConfigurer,但继承WebMvcConfigurationSupport类是会导致自动配置失效的。
    //
    //这是因为在 springboot的web自动配置类 WebMvcAutoConfiguration 上有条件注解
    //
    //@ConditionalOnMissingBean(WebMvcConfigurationSupport.class)
    //
    //这个注解的意思是在项目类路径中 缺少 WebMvcConfigurationSupport类型的bean时改自动配置类才会生效,所以继承 WebMvcConfigurationSupport 后需要自己再重写相应的方法。
    //
    //这时候就需要重新指定静态资源

@Autowired
private RepeatInterceptor repeatInterceptor;
@Override
public void addInterceptors(InterceptorRegistry registry)
{
    
    
    registry.addInterceptor(repeatInterceptor)
            .excludePathPatterns("/doc.html");

}

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
    
    
        registry.addResourceHandler("/**").addResourceLocations(
                "classpath:/static/");
        registry.addResourceHandler("doc.html").addResourceLocations(
                "classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations(
                "classpath:/META-INF/resources/webjars/");
        super.addResourceHandlers(registry);
    }

}

再次访问项目路径的doc.html:
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_41358574/article/details/121487368
今日推荐