Springdoc 2.8.15与 Spring Boot 3.5.11无法同时工作

后端开发 2026-07-12

我正在运行Springdoc 2.8.15和 Spring Boot 3.5.11。只要我不使用 @Parameter和 @OpenApiDefinition注解,API规范就能正确生成。如果使用它们中的任意一个,就会收到如下错误:

{"code":"SERVLET","message":"Handler dispatch failed: java.lang.NoSuchMethodError: 'io.swagger.v3.oas.annotations.media.Schema$RequiredMode io.swagger.v3.oas.annotations.media.Schema.requiredMode()'"}

Gradle:

plugins {
  java
  idea
  jacoco
  id("org.springframework.boot") version "3.5.11"
  id("io.spring.dependency-management") version "1.1.7"
}
dependencies {
    implementation("org.springframework.boot:spring-boot-starter-actuator")
    implementation("org.springframework.boot:spring-boot-starter-web")
    implementation("org.springframework.kafka:spring-kafka")
    implementation("org.springframework.boot:spring-boot-starter-validation")
    implementation("org.springdoc:springdoc-openapi-starter-webmvc-ui:2.8.15")
}

API:

@Tag(
    name = "Frequency API",
    description = "Controls the frequency of the simulation"
)
@RequestMapping("/api/v1/frequency")
public interface FrequencyApi {

  /**
   * Set the frequency of the simulation. A value of 0 means the simulation stops.
   *
    * @param frequency The frequency to set (0 <= frequency <= 100_000)
   */
  @Operation(
      summary = "Set the simulation frequency",
      description = "Sets the frequency for the simulation. A value of 0 means the simulation stops. Max value is 100.000"
  )
  @PutMapping("/{frequency}")
  @ResponseStatus(code = NO_CONTENT)
  void putFrequency(
      @Parameter(description = "The frequency to be set", required = true, example = "10")
      @Min(0)
      @Max(100_000)
      @PathVariable("frequency") int frequency
  );

解决方案

你可以尝试强制Maven使用这个版本,以及你可能正在使用的其他Swagger依赖。

<dependency>
    <groupId>io.swagger.core.v3</groupId>
    <artifactId>swagger-annotations</artifactId>
    <version>2.2.28</version>
</dependency>

备选方案

以下配置在与Gradle 8搭配使用时最终也能工作:

plugins {
  java
  idea
  jacoco
  id("org.springframework.boot") version "3.4.4"
  id("io.spring.dependency-management") version "1.1.7"
}
dependencies {
    implementation("org.springframework.boot:spring-boot-starter-actuator")
    implementation("org.springframework.boot:spring-boot-starter-web")
    implementation("org.springframework.kafka:spring-kafka")
    implementation("org.springframework.boot:spring-boot-starter-validation")
    implementation("org.springdoc:springdoc-openapi-starter-webmvc-ui:2.7.0")
    implementation("io.swagger.core.v3:swagger-annotations:2.2.27")
}
站内所有文章版权归属LeftHeroAI导航站,无授权禁止任何主体转载、抄袭、复制内容,亦不得私自架设镜像站点。一经侵权,本站将通过法律途径追责。

相关文章