Spring Boot + Tomcat + Jetty - 应用程序启动失败

Spring Boot + Tomcat + Jetty - Application failed to start

我正在使用 Spring Boot + Java 8. 添加了 REST 资源并尝试测试以确保基本配置正确。

但是我在启动 Spring 引导应用程序时遇到问题。请找到以下日志。

021-01-15 00:19:53.320  WARN 22065 --- [           main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is java.lang.NoSuchMethodError: org.apache.tomcat.util.ExceptionUtils.preload()V
2021-01-15 00:19:53.329  INFO 22065 --- [           main] ConditionEvaluationReportLoggingListener : 

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2021-01-15 00:19:53.347 ERROR 22065 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

An attempt was made to call a method that does not exist. The attempt was made from the following location:

    org.apache.catalina.startup.Tomcat.<init>(Tomcat.java:183)

The following method did not exist:

    org.apache.tomcat.util.ExceptionUtils.preload()V

The method's class, org.apache.tomcat.util.ExceptionUtils, is available from the following locations:

    jar:file:/home/xyz/.gradle/caches/modules-2/files-2.1/org.eclipse.jetty/jetty-runner/9.3.20.v20170531/56d1d067b4e42a6c603ece754534f9a65211924a/jetty-runner-9.3.20.v20170531.jar!/org/apache/tomcat/util/ExceptionUtils.class
    jar:file:/home/xyz/.gradle/caches/modules-2/files-2.1/org.apache.tomcat.embed/tomcat-embed-core/9.0.41/a43e9711e85073187d04b137882b4b7957180ef0/tomcat-embed-core-9.0.41.jar!/org/apache/tomcat/util/ExceptionUtils.class

The class hierarchy was loaded from the following locations:

    org.apache.tomcat.util.ExceptionUtils: file:/home/xyz/.gradle/caches/modules-2/files-2.1/org.eclipse.jetty/jetty-runner/9.3.20.v20170531/56d1d067b4e42a6c603ece754534f9a65211924a/jetty-runner-9.3.20.v20170531.jar


Action:

Correct the classpath of your application so that it contains a single, compatible version of org.apache.tomcat.util.ExceptionUtils

我尝试排除 tomcat,但仍然遇到同样的问题。

请查看下面我的 Gradle 设置。

plugins {
    id 'org.springframework.boot' version '2.4.1'
    id 'io.spring.dependency-management' version '1.0.10.RELEASE'
    id 'java'
}

group = 'com.hive'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
targetCompatibility = '1.8'

configurations {
    all*.exclude module: 'log4j'
    all*.exclude module: 'log4j-core'
    all*.exclude module: 'slf4j-log4j12'
    all*.exclude module: 'log4j-slf4j-impl'
}

repositories {
    mavenCentral()
}

dependencies {

    compile group: 'org.apache.commons', name: 'commons-dbcp2', version: '2.7.0'
    compile group: 'org.apache.hive', name: 'hive-jdbc', version: '3.1.2'

    compile group: 'org.springframework.boot', name: 'spring-boot-starter-web'
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-jdbc'

    compileOnly 'org.projectlombok:lombok'
    annotationProcessor 'org.projectlombok:lombok'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

test {
    useJUnitPlatform()
}

请给我一些解决上述依赖问题的方法。

我想这可能是因为您没有将 gradle 插件添加到您的构建中,您可以通过以下方式添加:

plugins {
id "org.springframework.boot" version "2.0.1.RELEASE"
}

如果您使用的 Gradle 版本早于 2.1 或者您需要动态配置,您可以这样添加:

buildscript {
ext {
    springBootVersion = '2.0.1.RELEASE'
}
repositories {
    mavenCentral()
}
dependencies {
    classpath(
      "org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}

apply plugin: 'org.springframework.boot'

让我知道这是否有效。

问题出在 hive-jdbc 驱动程序上,它下载了发生依赖冲突的 jetty-runner。

从 hive-jdbc 中排除 jetty-runner 后,上述冲突问题得到解决。

    compile(group: 'org.apache.hive', name: 'hive-jdbc', version: '3.1.2') {
        exclude(group: 'org.eclipse.jetty', module: 'jetty-runner')
    }