从tomcat迁移到undertow org.springframework.web.servlet库问题

klr1opcd  于 2021-10-10  发布在  Java
关注(0)|答案(1)|浏览(494)

我搜索了stack和google,寻找“运气不好”的答案。所以我希望有人能帮我。
我有一个SpringBootAPI,目前正在使用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:

如果有人能给我点启示的话,非常感谢。

更新

正在添加my 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) }
        }
    }
}
3b6akqbq

3b6akqbq1#

排除 spring-boot-starter-web 您确实排除了它的所有依赖项,这些依赖项是在servlet环境中运行spring引导项目所必需的。最值得注意的是,你确实排除了 spring-web ,其中包含在错误消息中找到的大多数类。
顾名思义 spring-boot-starter-web 不是以tomcat为中心,而是它的依赖关系 spring-boot-starter-tomcat 是因此,您应该排除后一个工件,并包括 spring-boot-starter-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> 随时标记。SpringBoot管理许多工件的版本(请参阅托管依赖项的完整列表)。
编辑:如果使用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'
    ...
}

相关问题