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

Spring整合Swagger/OpenAPI报错求助:数据源与依赖兼容问题

Spring Boot集成Swagger/OpenAPI问题排查与修复

1. 数据源配置错误修复

  • 问题根源:Spring Boot自动配置数据源时,既没找到数据库URL等核心配置,又没有引入嵌入式数据库依赖,导致无法确定驱动类。
  • 修复方案:
    • 仅开发/测试用:添加H2嵌入式数据库依赖,让Spring Boot自动配置数据源
      <dependency>
          <groupId>com.h2database</groupId>
          <artifactId>h2</artifactId>
          <scope>development</scope>
      </dependency>
      
    • 用真实数据库:在application.properties补全完整配置
      spring.datasource.url=jdbc:mysql://localhost:3306/your_database_name
      spring.datasource.username=your_db_user
      spring.datasource.password=your_db_pwd
      spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
      
    • 项目暂不需要数据源:在启动类NorthwindApplication.java上排除数据源自动配置
      @SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
      public class NorthwindApplication {
          public static void main(String[] args) {
              SpringApplication.run(NorthwindApplication.class, args);
          }
      }
      

2. spring-plugin-core与spring-core版本兼容问题

  • 问题根源:手动引入的spring-plugin-core版本和Spring Boot自带的spring-core版本不匹配,导致org.springframework.util.Assert.notNull方法找不到。
  • 修复方案:
    • 删掉pom.xml中手动添加的spring-plugin-core依赖,让Spring Boot的spring-boot-starter-parent自动管理依赖版本,确保所有Spring生态依赖版本对齐。
    • 如果必须手动引入,要指定和当前Spring Boot版本兼容的版本:Spring Boot 2.x对应spring-plugin-core 2.x系列,Spring Boot 3.x对应3.x系列。

3. springdoc-openapi-ui 404错误修复

  • 问题根源:Spring Boot版本和springdoc依赖不匹配,或者访问路径错误、静态资源被拦截。
  • 修复方案:
    • 匹配Spring Boot版本选依赖:
      • Spring Boot 3.x:用springdoc-openapi-starter-webmvc-ui,访问路径是http://localhost:8080/swagger-ui/index.html
        <dependency>
            <groupId>org.springdoc</groupId>
            <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
            <version>2.2.0</version>
        </dependency>
        
      • Spring Boot 2.x:用springdoc-openapi-ui,访问路径是http://localhost:8080/swagger-ui.html
        <dependency>
            <groupId>org.springdoc</groupId>
            <artifactId>springdoc-openapi-ui</artifactId>
            <version>1.6.14</version>
        </dependency>
        
    • 检查静态资源拦截:如果用了Spring Security,要放行swagger相关路径,在配置类中添加:
      @Override
      protected void configure(HttpSecurity http) throws Exception {
          http.authorizeRequests()
                  .antMatchers("/swagger-ui/**", "/v3/api-docs/**")
                  .permitAll()
                  .anyRequest().authenticated();
      }
      
    • 确认application.properties没有禁用静态资源访问,无需额外配置的话保持默认即可。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.18 23:58:12