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

Webflux集成Swagger UI遇404问题求助

解决Webflux环境下Swagger UI 404问题

1. 替换依赖(推荐方案)

放弃springfox-boot-starter,改用对Webflux支持更完善、维护更活跃的springdoc-openapi-webflux-ui。

在pom.xml中添加依赖:

<dependency>
    <groupId>org.springdoc</groupId>
    <artifactId>springdoc-openapi-webflux-ui</artifactId>
    <version>2.2.0</version> <!-- Spring Boot 3.x适配2.x版本,Spring Boot 2.x适配1.x版本 -->
</dependency>

Gradle项目则添加:

implementation 'org.springdoc:springdoc-openapi-webflux-ui:2.2.0'

添加完成后启动项目,访问/swagger-ui.html即可正常打开Swagger UI。

2. 若坚持使用springfox的修复方案

如果一定要用springfox,需做好版本兼容并添加专属配置:

步骤1:匹配依赖版本

确保springfox-boot-starter版本与Spring Boot版本对应,比如Spring Boot 2.7.x对应springfox 3.0.0:

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-boot-starter</artifactId>
    <version>3.0.0</version>
</dependency>

步骤2:添加Webflux Swagger配置类

创建配置类,显式开启Webflux的Swagger支持:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.web.reactive.EnableSwaggerWebFlux;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

@Configuration
@EnableSwaggerWebFlux
public class SwaggerConfig {
    @Bean
    public Docket api() {
        return new Docket(DocumentationType.OAS_30)
                .select()
                .apis(RequestHandlerSelectors.basePackage("你的控制器根包路径"))
                .paths(PathSelectors.any())
                .build();
    }
}

步骤3:访问正确路径

启动项目后,访问/swagger-ui/index.html(springfox 3.x针对Webflux的专属路径)。

关键注意点

  • springfox对Webflux的兼容性一直存在问题,且项目维护频率低,优先推荐springdoc方案。
  • 检查项目中是否有自定义Webflux路由配置,避免覆盖Swagger静态资源路径。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.17 23:22:09