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

Spring Boot整合Swagger启动报错:UnsatisfiedDependencyException求助

解决Spring Boot 3.0.2整合Springfox Swagger的依赖冲突问题

问题根源

Springfox的2.9.2、3.0.0版本都不兼容Spring Boot 3.x(Spring Boot 3.x基于Spring Framework 6.x),org.springframework.util.comparator.InvertibleComparator这个类在Spring Framework 6.x中已被移除,所以启动时会触发依赖找不到的异常。

解决步骤

  • 移除Springfox相关依赖与注解

    1. 从pom.xml中删除所有springfox-swagger2、springfox-swagger-ui依赖
    2. 删掉启动类上的@EnableSwagger2注解
  • 替换为Spring官方支持的SpringDoc OpenAPI
    SpringDoc是Spring生态下官方推荐的OpenAPI文档工具,完全兼容Spring Boot 3.x。在pom.xml中添加以下依赖:

    <dependency>
        <groupId>org.springdoc</groupId>
        <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
        <version>2.2.0</version> <!-- 可根据最新稳定版调整 -->
    </dependency>
    

    启动项目后,直接访问http://localhost:端口号/swagger-ui/index.html就能看到API文档页面,无需额外添加启用注解。

  • 可选:自定义API文档配置
    如果需要配置API标题、描述、分组等,可添加如下配置类:

    import io.swagger.v3.oas.models.OpenAPI;
    import io.swagger.v3.oas.models.info.Info;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    @Configuration
    public class OpenApiConfig {
        @Bean
        public OpenAPI customOpenAPI() {
            return new OpenAPI()
                    .info(new Info()
                            .title("项目API文档")
                            .version("1.0")
                            .description("项目接口的详细说明"));
        }
    }
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.29 03:35:00