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

Spring Boot 3.3升级org.webjars:swagger-ui 5.20.7后无法访问Swagger

Spring Boot 3.3 + Swagger UI 5.20.7 访问异常解决方案

问题背景

应用基于Spring Boot 3.3,无法升级至3.4版本;手动将org.webjars:swagger-ui版本调整为5.20.7后,Swagger页面无法访问,报错信息:No static resource swagger-ui/index.html。当前使用springdoc-openapi-starter-webmvc-ui:2.6.0,这是兼容Spring Boot 3.3的最新版本,且该版本官方兼容的swagger-ui最高版本为5.21.0。

可行解决步骤

1. 修正静态资源映射配置

swagger-ui 5.x版本的静态资源目录结构与旧版本存在差异,springdoc默认的资源映射可能无法匹配新结构。可通过配置类手动指定映射规则:

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class SwaggerResourceConfig implements WebMvcConfigurer {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/swagger-ui/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/swagger-ui/5.20.7/");
    }
}

也可通过配置文件实现(以application.yml为例):

spring:
  web:
    resources:
      static-locations: classpath:/META-INF/resources/webjars/swagger-ui/5.20.7/

2. 排除冲突依赖并显式声明版本

确保springdoc不会引入自带的低版本swagger-ui,在pom.xml中做如下配置:

<dependency>
    <groupId>org.springdoc</groupId>
    <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
    <version>2.6.0</version>
    <exclusions>
        <exclusion>
            <groupId>org.webjars</groupId>
            <artifactId>swagger-ui</artifactId>
        </exclusion>
    </exclusions>
</dependency>

<dependency>
    <groupId>org.webjars</groupId>
    <artifactId>swagger-ui</artifactId>
    <version>5.20.7</version>
</dependency>

3. 验证访问路径

swagger-ui 5.x的默认访问路径可能有所调整,尝试访问以下地址确认:

  • http://{host}:{port}/swagger-ui/index.html
  • http://{host}:{port}/swagger-ui/

4. 清理依赖缓存并重建项目

  • 清理本地Maven/Gradle仓库中旧的swagger-ui相关依赖包
  • 执行mvn clean install(Maven)或对应Gradle命令,重新构建项目,避免缓存导致的依赖冲突

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.13 04:37:05