Spring Boot + Tomcat + Jetty - 应用程序无法启动

Posted

技术标签:

【中文标题】Spring Boot + Tomcat + Jetty - 应用程序无法启动【英文标题】:Spring Boot + Tomcat + Jetty - Application failed to start 【发布时间】:2021-04-19 15:33:39 【问题描述】:

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

但我在启动 Spring Boot 应用程序时遇到了问题。请查找以下日志。

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()


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

【问题讨论】:

【参考方案1】:

问题在于 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')
    

【讨论】:

谢谢,我也有类似的问题。这个解决方案有帮助:)【参考方案2】:

我想这可能是因为你没有在你的构建中添加 gradle 插件,你可以这样做:

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

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

buildscript 
ext 
    springBootVersion = '2.0.1.RELEASE'

repositories 
    mavenCentral()

dependencies 
    classpath(
      "org.springframework.boot:spring-boot-gradle-plugin:$springBootVersion")



apply plugin: 'org.springframework.boot'

让我知道这是否有效。

【讨论】:

如果您查看我上面的 gradle 设置,我已经添加了插件。而且我使用的是 gradle 版本 6.7。

以上是关于Spring Boot + Tomcat + Jetty - 应用程序无法启动的主要内容,如果未能解决你的问题,请参考以下文章

Spring Boot启动过程:Spring Boot内嵌Tomcat启动

Spring Boot配置Tomcat

在 Tomcat 上使用 Spring Boot 进行身份验证

Spring Boot 应用程序使用 spring-boot-starter-actuator 给出“无法启动 tomcat”异常

spring-boot中tomcat的默认连接池?

在 Spring Boot 上更改嵌入式 tomcat 版本