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

Spring Boot 2.4.13整合Swagger 3.0.0启动报错求解决方案

Spring Boot 2.4.13集成Swagger 3.0.0报错的根因与修复

根因分析

这个错误的核心是Springfox 3.0.0与Spring Boot 2.4.x版本存在兼容性问题,具体表现为Springfox内部依赖的Spring Integration通道inMemorySwaggerResourcesProvider-out-0没有订阅者。原因包括:

  1. springfox-boot-starter已默认包含springfox-swagger-ui依赖,重复添加会引发依赖冲突或组件初始化异常;
  2. Springfox 3.0.0的自动配置逻辑在Spring Boot 2.4.x环境下,无法正确完成Spring Integration相关组件的订阅绑定,导致消息发送时找不到接收者。

修复方案

方案1:简化依赖配置(优先尝试)

删除单独添加的springfox-swagger-ui依赖,仅保留springfox-boot-starter即可,因为starter已经集成了swagger-ui及其他必要组件:

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

方案2:升级Springfox版本

如果方案1无效,将Springfox版本升级到3.0.1(该版本修复了部分与Spring Boot 2.4.x的兼容性问题):

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

方案3:手动配置Swagger并禁用自动配置

如果上述方案都不生效,可以通过手动配置替代自动配置,避免通道未订阅的问题:

  1. 创建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.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;

@Configuration
@EnableSwagger2WebMvc
public class SwaggerConfig {
    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.basePackage("你的controller包路径"))
                .paths(PathSelectors.any())
                .build();
    }
}
  1. 在application.properties中禁用Springfox的自动配置:
spring.autoconfigure.exclude=springfox.boot.starter.autoconfigure.SpringfoxWebMvcAutoConfiguration

验证修复

启动应用后,访问http://localhost:端口号/swagger-ui/index.html,若能正常打开Swagger页面且无报错日志,则修复成功。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.13 03:40:32