闲谈swagger2

我们在使用SpringBoot集成Swagger2中swagger-ui.html相关的所有前端静态文件都在springfox-swagger-ui-2.4.0.jar里面。

闲谈swagger2_第1张图片

SpringBoot自动配置本身并不会把/swagger-ui.html这个路径映射到对应的目录META-INF/resources/下面。我们加上这个映射即可。

继承WebMvcConfigurationSupport 复写addResourceHandlers(registry)方法添加映射路径并添加到spring容器中。

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

 如果你添加自定义拦截器,排除swagger映射端口

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 多个拦截器组成一个拦截器链
        // addPathPatterns 用于添加拦截规则
        // excludePathPatterns 用户排除拦截
        registry.addInterceptor(HandlerInterceptor)
                .addPathPatterns("/**")
                //剔除自定义规则
                //剔除swagger
                .excludePathPatterns(
                        "/swagger-resources/**",
                        "/webjars/**",
                        "/v2/**",
                        "/swagger-ui.html/**"
                );

        super.addInterceptors(registry);
    }

 

你可能感兴趣的:(海豚湾的等待)