关于Swagger中访问不了文档页面

在这里插入图片描述

因为在SpringBoot启动类中,没有加上@EnableSwagger2WebMvc注解,这个注解的作用是启用swagger对应用程序暴露的API端点进行文档化,个人推断和拦截器拦截请求有关,解决办法就是加@EnableSwagger2WebMvc注解在SpringBoot启动类上,如图

@EnableSwagger2WebMvc
@SpringBootApplication
@MapperScan("com.atguigu.ssyx.product.mapper")
public class ServiceProductApplication {
    public static void main(String[] args) {
        SpringApplication.run(ServiceProductApplication.class);
    }
}

===================== 分割线 ==========================

可能有另一种情况,在网页访问时,出现doc.html404的情况
在这里插入图片描述
并且在控制台也报错404

2023-06-22 23:21:59.097  INFO 26700 --- [nio-8211-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 34 ms
2023-06-22 23:21:59.146  WARN 26700 --- [nio-8211-exec-1] o.s.web.servlet.PageNotFound             : No mapping for GET /doc.html
2023-06-22 23:22:00.837  INFO 26700 --- [  restartedMain] d.s.w.p.DocumentationPluginsBootstrapper : Documentation plugins bootstrapped

出现这种情况可能是项目中含有继承WebMvcConfigrationSupport的类,当在配置拦截器时,可以选择WebMvcConfigurationSupport,或者是WebMvcConfigurer,但是使用前者会导致自动配置失效,即WebMvcConfigurationSupprt会使SpringBoot的WebMvc自动配置失效,倒是视图解析器无法解析并返回到对应的视图,因为在springboot的web自动配置类WebMvcAutoConfiguration上含有条件注解@ConditionalOnMissingBean(WebMvcConfigurationSupport.class)有关。该注解表示在项目类路径中如果没有WebConfigurationSupport类型的bean时,自动配置才会生效。

这时需要重新指定静态资源

对应的knife4j的Ui图路径如下(classpath:/META-INF/resources/)

在这里插入图片描述

需要在继承了WebMvcConfigurationSupport的类上重写addResourceHandlers方法,用于在SpringMMVC中配置该静态资源的处理器

在这里插入图片描述
相关代码如下:

  @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**")
                .addResourceLocations("classpath:/META-INF/resources/");
        super.addResourceHandlers(registry);
    }
  • 8
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值