从 Tomcat 迁移到 Undertow org.springframework.web.servlet 库问题

Migrating from Tomcat to Undertow org.springframework.web.servlet lib issues

我搜索了 Stack 和 google 寻找运气不好的答案。所以我希望有人能在这里帮助我。

我有一个 Spring 引导 API,目前正在使用 Tomcat,现在我已经阅读了关于 Undertow 的一些性能改进,所以我想试一试亲眼看看。

现在,我删除了我的 spring-boot-web-starter 依赖项并添加了 undertow 但是我在一些 类 中遇到了以下错误而且我似乎无法找到解决方法:

class file for org.springframework.web.servlet.HandlerExceptionResolver not found
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
java: package org.springframework.web.servlet.config.annotation does not exist
java: package org.springframework.web.servlet.config.annotation does not exist
  symbol: class AbstractMappingJacksonResponseBodyAdvice

我试过添加 spring-mvc 依赖项,它确实解决了上述错误,但随后导致了另一种类型的错误:

Description:

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

    org.springframework.web.servlet.handler.AbstractHandlerMapping.<init>(AbstractHandlerMapping.java:83)

The following method did not exist:

    'org.apache.commons.logging.Log org.springframework.core.log.LogDelegateFactory.getHiddenLog(java.lang.String)'

The method's class, org.springframework.core.log.LogDelegateFactory, is available from the following locations:

如果有人能为我提供一些帮助,非常感谢。

** 更新 **

正在添加我的 build.gradle 文件:

plugins {
    id "org.springframework.boot" version "2.4.3"
    id 'io.spring.dependency-management' version '1.0.11.RELEASE'
    id 'java'
    id "com.commercehub.gradle.plugin.avro" version "0.21.0"
    id "idea"
}
group 'org.test.profile-service'
version '1.0'

java {
    sourceCompatibility = JavaVersion.VERSION_14
    targetCompatibility = JavaVersion.VERSION_14
}

ext {
    avroVersion = "1.10.1"
}

repositories {
    mavenCentral()
    jcenter()
    maven {
        url "https://packages.confluent.io/maven/"
    }
}

avro {
    createSetters = true
    fieldVisibility = "PRIVATE"
}

//apply plugin: "war"

dependencies {
//    providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat'

    compile group: 'co.elastic.logging', name: 'logback-ecs-encoder', version: '0.5.2'
    compile group: 'com.amazonaws', name: 'aws-java-sdk', version: '1.11.860'

    compile group: 'io.springfox', name: 'springfox-swagger-ui', version: '3.0.0'
    compile group: 'io.springfox', name: 'springfox-boot-starter', version: '3.0.0'

    compile('org.springframework.boot:spring-boot-starter-data-elasticsearch')
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-data-mongodb', version: '2.3.3.RELEASE'
    compile group: 'org.springframework.data', name: 'spring-data-elasticsearch', version: '4.1.5'
//    compile group: 'org.springframework.boot', name: 'spring-boot-starter-data-jpa', version: '2.3.3.RELEASE'
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-security', version: '2.3.3.RELEASE'
    compile group: 'org.springframework.security', name: 'spring-security-oauth2-client', version: '5.4.0'
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-validation', version: '2.4.2'
    compile group: 'org.springframework.kafka', name: 'spring-kafka', version: '2.6.5'
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-oauth2-resource-server', version: '2.4.2'
    implementation group: 'org.springframework.boot', name: 'spring-boot-starter-cache', version: '2.4.3'
    implementation 'org.springframework.boot:spring-boot-starter-data-redis'

    compile group: 'javax.validation', name: 'validation-api', version: '2.0.1.Final'
    compile group: 'com.fasterxml.jackson.datatype', name: 'jackson-datatype-jsr310', version: '2.11.2'


    compile group: 'io.jsonwebtoken', name: 'jjwt', version: '0.9.1'
    implementation group: 'org.json', name: 'json', version: '20201115'

    compile group: 'org.openapitools', name: 'jackson-databind-nullable', version: '0.2.1'

    compile group: 'commons-io', name: 'commons-io', version: '2.6'
    compile group: 'org.apache.commons', name: 'commons-collections4', version: '4.4'
    compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.11'
    compile group: 'org.passay', name: 'passay', version: '1.6.0'
    compile group: 'com.google.guava', name: 'guava', version: '30.0-jre'

    // https://mvnrepository.com/artifact/com.auth0/java-jwt
    compile group: 'com.auth0', name: 'java-jwt', version: '3.12.0'


    compile group: 'io.confluent', name: 'kafka-schema-registry-client', version: '6.0.0'
    compile group: 'io.confluent', name: 'kafka-avro-serializer', version: '6.0.0'
    compile group: 'io.confluent', name: 'monitoring-interceptors', version: '6.0.0'
    compile(group: 'io.confluent', name: 'kafka-streams-avro-serde', version:'6.0.0') {
        exclude(module: 'log4j-over-slf4j')
    }

    compile "org.apache.avro:avro:1.10.1"
    implementation "org.apache.avro:avro:${avroVersion}"

    compileOnly 'org.projectlombok:lombok:1.18.12'
    annotationProcessor 'org.projectlombok:lombok:1.18.12'

    implementation 'com.amazonaws:aws-java-sdk-s3'
    implementation 'org.springframework.boot:spring-boot-starter-web'


    testCompile group: 'junit', name: 'junit', version: '4.12'
    testCompileOnly 'org.projectlombok:lombok:1.18.12'
    testAnnotationProcessor 'org.projectlombok:lombok:1.18.12'

    testImplementation('org.springframework.boot:spring-boot-starter-test') {
        exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
    }

    jar {
        manifest {
            attributes(
                    'Main-Class': 'org.test.SpringBootPersistenceApplication'
            )
        }
        from {
            configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
        }
    }
}

通过排除 spring-boot-starter-web,您确实排除了它的所有依赖项,这些依赖项对于 运行 Spring 在 servlet 环境中启动项目是必需的。最值得注意的是,您确实排除了 spring-web,其中包含您在错误消息中找到的大部分 类。

顾名思义,spring-boot-starter-web 并不以 Tomcat 为中心,只有它的依赖项 spring-boot-starter-tomcat 才是。所以你应该排除后一个工件并包含 spring-boot-starter-undertow 以将必要的 Undertow 依赖项拉入项目:

  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.4.5</version>
  </parent>
  ...
  <dependencies>
    ...
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
      <exclusions>
        <exclusion>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-tomcat</artifactId>
        </exclusion>
      </exclusions>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-undertow</artifactId>
    </dependency>
  </dependencies>

最后一个错误可能是由于您的项目中 Spring Boot 的多个不兼容版本。为确保您拥有兼容的版本,请将项目的父 POM 设置为 spring-boot-starter-parent(或 spring-boot-dependencies)并尽可能省略指定 <version> 标记。 Spring Boot 管理许多工件的版本(请参阅托管依赖项的 complete list)。

编辑: 如果您使用的是 Gradle,您的 build.gradle 应该如下所示:

plugins {
    id 'org.springframework.boot' version '2.4.5'
    id 'io.spring.dependency-management' version '1.0.11.RELEASE'
    ...
}

dependencies {
    implementation('org.springframework.boot:spring-boot-starter-web') {
        exclude group: 'org.springframework.boot', module: 'spring-boot-starter-tomcat'
    }
    implementation 'org.springframework.boot:spring-boot-starter-undertow'
    ...
}