首页 > 解决方案 > 基于 Springboot 的 REST API 的开放 API 文档因 ApplicationContextException 而失败。该项目是用 gradlew shadow 插件构建的

问题描述

我正在尝试将打开的 api 文档添加到我拥有的 springboot 基础 REST api 项目中。在 intelliJ IDE 本地运行或使用 gradlew run/bootRun 时,它工作正常

但是,当使用 gradlew shadow 插件“com.github.johnrengelman.shadow”将项目打包为胖 jar 并在命令行上运行时,java -jar build/libs/Application-0.0.1-SNAPSHOT-all.jar它失败了ApplicationContextException

异常堆栈跟踪:

Caused by: org.springframework.context.ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean.
        at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.getWebServerFactory(ServletWebServerApplicationContext.java:205) ~[Application-0.0.1-SNAPSHOT-all.jar:?]
        at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.createWebServer(ServletWebServerApplicationContext.java:177) ~[Application-0.0.1-SNAPSHOT-all.jar:?]
        at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:158) ~[Application-0.0.1-SNAPSHOT-all.jar:?]
        ... 9 more

构建.gradle

plugins {
    id 'org.springframework.boot' version '2.3.4.RELEASE'
    id 'io.spring.dependency-management' version '1.0.9.RELEASE'
    id 'application'
    id 'com.github.johnrengelman.shadow' version "6.0.0"
}
application {
    mainClassName = 'com.example.Application'
    executableDir = 'lib/'
}
dependencies {
    implementation 'org.springdoc:springdoc-openapi-ui:1.5.12'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation files( "lib/$JarV2" )
    compileOnly 'org.projectlombok:lombok:1.18.22'
    annotationProcessor 'org.projectlombok:lombok:1.18.22'
    implementation 'org.slf4j:slf4j-api:1.7.32'
    implementation 'org.springframework.boot:spring-boot-starter-validation:2.3.3.RELEASE'
    implementation 'org.springframework.boot:spring-boot-starter-log4j2:2.4.2'
    implementation 'org.apache.logging.log4j:log4j-slf4j-impl:2.14.0'
    implementation 'org.apache.logging.log4j:log4j-api:2.14.0'
    implementation 'org.apache.logging.log4j:log4j-core:2.14.0'
    implementation 'org.slf4j:jul-to-slf4j:1.7.30'
    implementation 'commons-lang:commons-lang:2.6'
    implementation "com.moandjiezana.toml:toml4j:0.7.2"
    implementation "io.jsonwebtoken:jjwt-api:$jwtVersion"
    runtimeOnly "io.jsonwebtoken:jjwt-impl:$jwtVersion"
    runtimeOnly "io.jsonwebtoken:jjwt-jackson:$jwtVersion"
    implementation 'com.auth0:java-jwt:3.18.1'
    implementation 'com.auth0:jwks-rsa:0.19.0'

    testImplementation('org.springframework.boot:spring-boot-starter-test')
}

没有 openapi 依赖 implementation 'org.springdoc:springdoc-openapi-ui:1.5.12'

独立运行java -jar build/libs/Application-0.0.1-SNAPSHOT-all.jar绝对正常。

open api 和 springboot 库之间是否存在一些依赖冲突?

标签: javaspring-bootspringdoc-openapi-uigradle-shadow-plugin

解决方案


原来是已知问题,可以通过在 build.gradle 中包含以下内容来解决

import com.github.jengelman.gradle.plugins.shadow.transformers.*

shadowJar {

zip64 true

exclude 'META-INF/*.SF'
exclude 'META-INF/*.DSA'
exclude 'META-INF/*.RSA'

// Required for Spring
mergeServiceFiles()
transform(AppendingTransformer) { resource = 'reference.conf'  }
transform(AppendingTransformer) { resource = 'META-INF/spring.handlers'  }
transform(AppendingTransformer) { resource = 'META-INF/spring.schemas'  }
transform(AppendingTransformer) { resource = 'META-INF/spring.tooling'  }
transform(PropertiesFileTransformer) {
    paths = ['META-INF/spring.factories' ]
    mergeStrategy = "append"
}
}

来源:https ://github.com/spring-projects/spring-boot/issues/1828


推荐阅读