Spring boot Swagger UI启动时出现异常

问题描述 投票:0回答:2

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

上下文初始化期间遇到异常 - 取消 刷新尝试: org.springframework.beans.factory.UnsatisfiedDependencyException: 创建名称为“securityConfig”的 bean 时出错:依赖关系不满足 通过方法'setContentNegotationStrategy'参数0表示; 嵌套异常是 org.springframework.beans.factory.UnsatisfiedDependencyException: 使用名称创建 bean 时出错 'org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration$EnableWebMvcConfiguration': 通过“setConfigurers”方法表达的依赖关系未得到满足 参数0;嵌套异常是 org.springframework.beans.factory.UnsatisfiedDependencyException: 创建名为“swaggerWebMvcConfigurer”的 bean 时定义的错误 类路径资源[org/springdoc/webmvc/ui/SwaggerConfig.class]: 通过方法表达的不满足的依赖关系 'swaggerWebMvcConfigurer' 参数 1;嵌套异常是 org.springframework.beans.factory.UnsatisfiedDependencyException: 创建类中定义的名为“indexPageTransformer”的 bean 时出错 路径资源[org/springdoc/webmvc/ui/SwaggerConfig.class]: 通过“indexPageTransformer”方法表达的依赖关系未得到满足 参数3;嵌套异常是 org.springframework.beans.factory.BeanCreationException:错误 创建类路径中定义的名为“swaggerWelcome”的 bean 资源[org/springdoc/webmvc/ui/SwaggerConfig.class]: 合并bean定义的后处理失败;嵌套异常是 java.lang.IllegalStateException:无法自省类 来自 ClassLoader 的 [org.springdoc.webmvc.ui.SwaggerWelcomeWebMvc] [jdk.internal.loader.ClassLoaders$AppClassLoader@251a69d7]

我将 Kotlin 与 Spring Boot 结合使用,这些是我的 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 spring-boot kotlin swagger-ui springdoc-openapi-ui
2个回答
17
投票

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
似乎没有相应的里程碑版本。


© www.soinside.com 2019 - 2024. All rights reserved.