Spring 启动 Swagger UI 启动时出现异常

Spring boot Swagger UI exception at startup

我正在尝试让 Swagger UI 在我的项目中工作并且我正在关注 these docs,但是无论我尝试什么我的应用程序都会立即崩溃并出现以下异常:

Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'securityConfig': Unsatisfied dependency expressed through method 'setContentNegotationStrategy' parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration$EnableWebMvcConfiguration': Unsatisfied dependency expressed through method 'setConfigurers' parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'swaggerWebMvcConfigurer' defined in class path resource [org/springdoc/webmvc/ui/SwaggerConfig.class]: Unsatisfied dependency expressed through method 'swaggerWebMvcConfigurer' parameter 1; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'indexPageTransformer' defined in class path resource [org/springdoc/webmvc/ui/SwaggerConfig.class]: Unsatisfied dependency expressed through method 'indexPageTransformer' parameter 3; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'swaggerWelcome' defined in class path resource [org/springdoc/webmvc/ui/SwaggerConfig.class]: Post-processing of merged bean definition failed; nested exception is java.lang.IllegalStateException: Failed to introspect Class [org.springdoc.webmvc.ui.SwaggerWelcomeWebMvc] from ClassLoader [jdk.internal.loader.ClassLoaders$AppClassLoader@251a69d7]

我将 Kotlin 与 Spring 引导一起使用,这些是我的 Gradle 依赖项:

implementation("org.springframework.boot:spring-boot-starter-web")
implementation("org.springframework.boot:spring-boot-starter-jooq")
implementation("org.springframework.boot:spring-boot-starter-security")
implementation("org.springframework.boot:spring-boot-configuration-processor")
implementation("org.springframework.boot:spring-boot-starter-validation")
implementation("org.springframework.boot:spring-boot-starter-log4j2")
implementation("com.braintreepayments.gateway:braintree-java:3.14.0")
implementation("com.fasterxml.jackson.module:jackson-module-kotlin:2.13.1")
implementation("com.fasterxml.jackson.core:jackson-databind:2.13.2")
implementation("com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:2.13.2")
implementation("org.apache.logging.log4j:log4j-layout-template-json")
implementation("org.springdoc:springdoc-openapi-kotlin:1.6.6")
implementation("org.springdoc:springdoc-openapi-ui:1.6.6")
implementation("org.jooq:jooq-meta")
implementation("org.jooq:jooq-kotlin")
implementation("org.jooq:jooq-codegen")
implementation("org.jooq:jooq-codegen-maven")
implementation("org.jooq:jooq-meta-extensions")
implementation("org.jetbrains.kotlin:kotlin-reflect:1.6.10")
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.6.10")
implementation("com.zaxxer:HikariCP:5.0.1")
implementation("com.github.ben-manes.caffeine:caffeine:3.0.5")
implementation("org.flywaydb:flyway-core:8.5.4")
implementation("org.postgresql:postgresql:42.3.3")

我已经尝试了很多不同组合的不同包,但它总是会在启动时抛出异常。

Gradle 文件的插件部分:

plugins {
    id("org.springframework.boot") version "3.0.0-SNAPSHOT"
    id("io.spring.dependency-management") version "1.0.11.RELEASE"
    id("org.flywaydb.flyway") version "8.5.4"
    id("nu.studer.jooq") version "7.1.1"
    kotlin("jvm") version "1.6.20-RC"
    kotlin("plugin.spring") version "1.6.20-RC"
}

Spring Boot 3 将使用 Jakarta EE,而 Spring Boot 2 仍使用 Java EE。此移动包括将 Servlet API 的命名空间从 javax.servlet 更改为 jakarta.servlet。您的应用程序失败,因为 Springdoc 1.x 尝试从旧命名空间加载 类 但 Spring Boot 3 仅引入新命名空间。

Springdoc 发布了与 Jakarta EE 一起使用的里程碑版本和 Spring Boot 3 的里程碑版本:https://github.com/springdoc/springdoc-openapi/issues/1284

在 Java 应用程序中,您可以替换

implementation 'org.springdoc:springdoc-openapi-ui:1.6.6'

implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.0.0-M1'

这里还有一个官方的Maven例子:https://github.com/springdoc/springdoc-openapi-demos/tree/2.x/demo-spring-boot-3-webmvc

如果您只将启动器包含到 Kotlin 项目中

implementation("org.springdoc:springdoc-openapi-starter-webmvc-ui:2.0.0-M1")

这似乎工作正常并生成 Swagger UI。但是目前似乎还没有 springdoc-openapi-kotlin.

对应的里程碑版本