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

Gradle 5.6升级至7.2后访问Swagger报Version不能为空问题咨询

问题根因分析

  • 缺少Spring Boot Gradle插件配置。当前项目为Spring Boot项目,但Gradle配置中未引入org.springframework.boot插件,导致Spring Boot核心配置(版本号注入、启动横幅所需的元数据、依赖版本自动管理)均未生效,是启动横幅无版本号的直接原因。
  • Swagger(SpringFox 2.9.x)与Spring Boot 2.5.x版本不兼容。SpringFox 2.9.2适配的是Spring Boot 2.1.x及更早版本,Spring Boot 2.5.x对Servlet上下文、资源处理逻辑的调整会导致SpringFox读取应用版本、接口元数据时失败,触发Version must not be null or empty异常,最终导致接口定义加载失败。
  • Gradle 7.x配置语法错误。你配置的implementation.canBeResolved = true属于错误用法:implementation本身是依赖声明配置,不具备可解析属性,强制设为可解析会导致runtimeClasspath的依赖筛选逻辑出错,部分Spring Boot元数据资源无法被打入最终产物,进一步加剧版本读取异常。
  • 自定义Jar打包逻辑存在拼写错误。你重写的Jar任务中,依赖筛选逻辑写的是configurations.runtimeClasspath-configuration.implementation,其中configuration少了末尾的s,会导致依赖筛选逻辑执行异常,部分必要资源文件被排除在Jar包外。

解决方案

1. 修正Gradle基础配置

  • 在plugins块中引入Spring Boot官方插件,统一管理依赖与打包逻辑:
plugins {
    id "org.springframework.boot" version "2.5.2"
    id "io.spring.dependency-management" version "1.0.11.RELEASE"
    // 保留原有sonarqube、gradle-git-properties等其他插件
}
  • 删除错误的configuration配置段:
// 完全删除这段配置
configurations {
  implementation.canBeResolved = true
}
  • 修正Jar任务拼写错误,优先使用Spring Boot官方bootJar任务打包可执行程序:
// 替换原有自定义jar块即可,无需手动处理依赖打入逻辑
bootJar {
    archiveVersion = "${project.findProperty('APP_VERSION') ?: 'MANUAL_BUILD'}"
    duplicatesStrategy = DuplicatesStrategy.EXCLUDE
}

// 如果sub2是公共库需要打普通Jar,保留jar任务并修正拼写即可
jar {
    archiveVersion = "${project.findProperty('APP_VERSION') ?: 'MANUAL_BUILD'}"
    duplicatesStrategy = DuplicatesStrategy.EXCLUDE
    from {
        // 修正configuration为configurations
        (configurations.runtimeClasspath - configurations.implementation).collect {
            it.isDirectory() ? it : zipTree(it)
        }
    }
}
  • 调整依赖声明,用官方starter替代零散依赖,避免版本冲突:
dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.aspectj:aspectjrt'
    // 移除原有手动声明的spring-webmvc、tomcat-embed-core依赖,由starter统一管理版本
}

2. 修复Swagger兼容问题

优先选择方案一,版本兼容更稳定:

  • 方案一:升级SpringFox到适配版本
    升级到SpringFox 3.0.0,该版本原生适配Spring Boot 2.4+,依赖修改为:
dependencies {
    implementation 'io.springfox:springfox-boot-starter:3.0.0'
}

升级后Swagger访问地址从原有http://host:port/swagger-ui.html调整为http://host:port/swagger-ui/index.html。

  • 方案二:保留Swagger 2.9.2,添加兼容配置
    若不方便升级Swagger版本,首先手动在Swagger配置类中指定版本,避免读取空值报错:
@Configuration
public class SwaggerConfig {
    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("你的接口业务包路径"))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("接口文档")
                .description("业务接口文档说明")
                .version("${APP_VERSION ?: '1.0.0'}") // 手动指定版本
                .build();
    }
}

其次在application配置中添加兼容项,恢复Spring Boot旧版路径匹配策略:

spring:
  mvc:
    pathmatch:
      matching-strategy: ant_path_matcher

3. 验证修复效果

重新构建项目启动,确认启动横幅正常展示Spring Boot版本号,访问Swagger地址确认接口文档正常加载即可。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.28 18:09:02