You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SpringBoot整合Swagger2后无法访问swagger-ui.html页面问题求助

SpringBoot集成Swagger访问空白页解决方案

1. 版本兼容问题修复

SpringBoot 2.6.x及以上版本默认路径匹配策略与Swagger不兼容

  • 在application.yml中添加如下配置:
spring:
  mvc:
    pathmatch:
      matching-strategy: ant_path_matcher
  • 若使用application.properties,配置如下:
spring.mvc.pathmatch.matching-strategy=ant_path_matcher

2. 静态资源拦截放行

项目中的拦截器、权限框架会拦截Swagger的静态资源导致页面无法加载

  • 若自定义了WebMvc配置类,添加资源映射规则:
@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/");
}
  • 若使用Spring Security等权限框架,需放行以下路径:
    /swagger-ui.html、/swagger-resources/**、/v2/api-docs、/v3/api-docs、/webjars/**

3. 扫描路径配置检查

检查Swagger配置类中apis(RequestHandlerSelectors.basePackage("xxx"))的包路径,必须为项目Controller所在的父包路径,路径错误会导致无接口可加载,页面空白。

4. 依赖冲突排查

  • Swagger2需要同时引入版本一致的springfox-swagger2和springfox-swagger-ui两个依赖,不可缺省
  • Swagger3只需引入springdoc-openapi-ui单个依赖即可,无需额外引入其他Swagger相关依赖,避免版本冲突
  • 若上述方案都无效,建议替换停止维护的SpringFox系列依赖,使用官方维护的SpringDoc OpenAPI依赖,适配所有主流SpringBoot版本,配置更简单。

内容的提问来源于stack exchange,提问作者Bieber

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.04 14:42:01