简体   繁体   中英

Open API documentation of Springboot based REST apis fail with ApplicationContextException. The project is built with gradlew shadow plugins

I am trying to add open api documentation to springboot base REST api project that I have. It works fine when run locally inside intelliJ IDE or with gradlew run/bootRun

But when the project is packaged as a fat jar using gradlew shadow plugin 'com.github.johnrengelman.shadow' and run on command line as java -jar build/libs/Application-0.0.1-SNAPSHOT-all.jar it fails with ApplicationContextException

Exception stacktrace:

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

build.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')
}

Without the openapi dependency implementation 'org.springdoc:springdoc-openapi-ui:1.5.12'

standalone run java -jar build/libs/Application-0.0.1-SNAPSHOT-all.jar works absolutely fine.

Is there some dependency conflict between open api and springboot libs ?

Turns out to be known issue and can be solved by including following in 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"
}
}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM