java - 从 Tomcat 迁移到 Undertow org.springframework.web.servlet lib 问题

标签 java spring-boot tomcat undertow

我在 Stack 和 google 上搜索过,寻找没有运气的答案。所以我希望有人可以帮助我。

我有一个 Spring Boot 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您确实排除了它的所有依赖项,这些依赖项是在 servlet 环境中运行 Spring Boot 项目所必需的。最值得注意的是,您确实排除了 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'
    ...
}

关于java - 从 Tomcat 迁移到 Undertow org.springframework.web.servlet lib 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67358216/

相关文章:

spring-boot - 带有自定义转换器的 Spring Boot 打破了 Swagger 以及如何移动它

java - 在 tomcat 上部署 grails 应用程序时创建名称为 'transactionManagerPostProcessor' 的 bean 时出错

java - 如果连接器端口为 8081,如何从 apache tomcat 中的 URL 中删除端口号

java - 如何禁用tomcat 7控制台调试信息

javascript - spring请求参数绑定(bind)错误

java - 如何提取json文件中的特定链接

spring - @EnableGlobalMethodSecurity 与 @EnableWebSecurity

mysql - Java 应用程序在亚马逊 AWS 中运行一段时间后停止运行

java - Eclipse 生成 equals 方法 : if ( obj == null ) vs. if ( null == obj )

java - proxyMode ScopedProxyMode.TARGET_CLASS 与 ScopedProxyMode.INTERFACE