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

Spring Boot 3.0.2 + Java17集成Springfox Swagger UI失败求助

Spring Boot 3 + Java 17 下Swagger UI 404问题解决

问题根源

Springfox 3.0.0不兼容Spring Boot 3.x版本,因为Spring Boot 3基于Jakarta EE API,而Springfox仍依赖旧的Java EE(javax.*)包,导致Swagger相关组件无法正常初始化,最终出现404错误。

解决方案步骤

1. 替换Gradle依赖

移除原Springfox依赖,改用支持Spring Boot 3的SpringDoc OpenAPI:

implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.2.0'

(注:版本号可根据最新稳定版调整,确保适配Spring Boot 3.x)

2. 移除旧的Swagger配置类

删除原BlApiUiApplication中的Docket Bean配置,SpringDoc会自动完成API文档的扫描和配置,无需手动创建Docket。修改后的启动类:

@SpringBootApplication
@ComponentScan({"com.bl.*"})
@EnableJpaRepositories(basePackages = { "com.bl.entity.repository" })
@EntityScan({"com.bl.entity"})
public class BlApiUiApplication {

    public static void main(String[] args) {
        SpringApplication.run(BlApiUiApplication.class, args);
    }
}

3. 调整Swagger UI访问地址

如果你的应用配置了上下文路径BLApiUI,正确的访问地址应为:

http://localhost:8080/BLApiUI/swagger-ui.html

或使用兼容路径:

http://localhost:8080/BLApiUI/swagger-ui/index.html

4. 可选:自定义API文档配置

如果需要类似原Docket的自定义配置(比如分组、API信息),可以使用SpringDoc的OpenAPI Bean替代:

@Bean
public OpenAPI customOpenAPI() {
    return new OpenAPI()
            .info(new Info()
                    .title("API")
                    .description("UI")
                    .version("1.0")
                    .license(new License().name("License").url("URL")));
}

5. 验证Controller注解兼容性

SpringDoc完全兼容Swagger 2的注解(@Api、@ApiOperation、@ApiResponses等),你原有的Controller代码无需修改,可直接使用。

验证

重启应用后,访问调整后的Swagger UI地址,即可正常加载API文档界面。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.02 15:10:39