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

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

我搜索了stack和google,寻找“运气不好”的答案。所以我希望有人能帮我。
我有一个SpringBootAPI,目前正在使用tomcat,现在我已经阅读了undertow中的一些性能改进,所以我想亲自去看看。
现在,我已经删除了对spring boot web starter的依赖性,并添加了undertow,但是我在一些类中遇到了以下错误,我似乎找不到如何解决它们:

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

我已经尝试添加spring mvc依赖性,它确实解决了上述错误,但随后会导致另一种类型的错误:

  1. Description:
  2. An attempt was made to call a method that does not exist. The attempt was made from the following location:
  3. org.springframework.web.servlet.handler.AbstractHandlerMapping.<init>(AbstractHandlerMapping.java:83)
  4. The following method did not exist:
  5. 'org.apache.commons.logging.Log org.springframework.core.log.LogDelegateFactory.getHiddenLog(java.lang.String)'
  6. The method's class, org.springframework.core.log.LogDelegateFactory, is available from the following locations:

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

更新

正在添加my build.gradle文件:

  1. plugins {
  2. id "org.springframework.boot" version "2.4.3"
  3. id 'io.spring.dependency-management' version '1.0.11.RELEASE'
  4. id 'java'
  5. id "com.commercehub.gradle.plugin.avro" version "0.21.0"
  6. id "idea"
  7. }
  8. group 'org.test.profile-service'
  9. version '1.0'
  10. java {
  11. sourceCompatibility = JavaVersion.VERSION_14
  12. targetCompatibility = JavaVersion.VERSION_14
  13. }
  14. ext {
  15. avroVersion = "1.10.1"
  16. }
  17. repositories {
  18. mavenCentral()
  19. jcenter()
  20. maven {
  21. url "https://packages.confluent.io/maven/"
  22. }
  23. }
  24. avro {
  25. createSetters = true
  26. fieldVisibility = "PRIVATE"
  27. }
  28. //apply plugin: "war"
  29. dependencies {
  30. // providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat'
  31. compile group: 'co.elastic.logging', name: 'logback-ecs-encoder', version: '0.5.2'
  32. compile group: 'com.amazonaws', name: 'aws-java-sdk', version: '1.11.860'
  33. compile group: 'io.springfox', name: 'springfox-swagger-ui', version: '3.0.0'
  34. compile group: 'io.springfox', name: 'springfox-boot-starter', version: '3.0.0'
  35. compile('org.springframework.boot:spring-boot-starter-data-elasticsearch')
  36. compile group: 'org.springframework.boot', name: 'spring-boot-starter-data-mongodb', version: '2.3.3.RELEASE'
  37. compile group: 'org.springframework.data', name: 'spring-data-elasticsearch', version: '4.1.5'
  38. // compile group: 'org.springframework.boot', name: 'spring-boot-starter-data-jpa', version: '2.3.3.RELEASE'
  39. compile group: 'org.springframework.boot', name: 'spring-boot-starter-security', version: '2.3.3.RELEASE'
  40. compile group: 'org.springframework.security', name: 'spring-security-oauth2-client', version: '5.4.0'
  41. compile group: 'org.springframework.boot', name: 'spring-boot-starter-validation', version: '2.4.2'
  42. compile group: 'org.springframework.kafka', name: 'spring-kafka', version: '2.6.5'
  43. compile group: 'org.springframework.boot', name: 'spring-boot-starter-oauth2-resource-server', version: '2.4.2'
  44. implementation group: 'org.springframework.boot', name: 'spring-boot-starter-cache', version: '2.4.3'
  45. implementation 'org.springframework.boot:spring-boot-starter-data-redis'
  46. compile group: 'javax.validation', name: 'validation-api', version: '2.0.1.Final'
  47. compile group: 'com.fasterxml.jackson.datatype', name: 'jackson-datatype-jsr310', version: '2.11.2'
  48. compile group: 'io.jsonwebtoken', name: 'jjwt', version: '0.9.1'
  49. implementation group: 'org.json', name: 'json', version: '20201115'
  50. compile group: 'org.openapitools', name: 'jackson-databind-nullable', version: '0.2.1'
  51. compile group: 'commons-io', name: 'commons-io', version: '2.6'
  52. compile group: 'org.apache.commons', name: 'commons-collections4', version: '4.4'
  53. compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.11'
  54. compile group: 'org.passay', name: 'passay', version: '1.6.0'
  55. compile group: 'com.google.guava', name: 'guava', version: '30.0-jre'
  56. // https://mvnrepository.com/artifact/com.auth0/java-jwt
  57. compile group: 'com.auth0', name: 'java-jwt', version: '3.12.0'
  58. compile group: 'io.confluent', name: 'kafka-schema-registry-client', version: '6.0.0'
  59. compile group: 'io.confluent', name: 'kafka-avro-serializer', version: '6.0.0'
  60. compile group: 'io.confluent', name: 'monitoring-interceptors', version: '6.0.0'
  61. compile(group: 'io.confluent', name: 'kafka-streams-avro-serde', version:'6.0.0') {
  62. exclude(module: 'log4j-over-slf4j')
  63. }
  64. compile "org.apache.avro:avro:1.10.1"
  65. implementation "org.apache.avro:avro:${avroVersion}"
  66. compileOnly 'org.projectlombok:lombok:1.18.12'
  67. annotationProcessor 'org.projectlombok:lombok:1.18.12'
  68. implementation 'com.amazonaws:aws-java-sdk-s3'
  69. implementation 'org.springframework.boot:spring-boot-starter-web'
  70. testCompile group: 'junit', name: 'junit', version: '4.12'
  71. testCompileOnly 'org.projectlombok:lombok:1.18.12'
  72. testAnnotationProcessor 'org.projectlombok:lombok:1.18.12'
  73. testImplementation('org.springframework.boot:spring-boot-starter-test') {
  74. exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
  75. }
  76. jar {
  77. manifest {
  78. attributes(
  79. 'Main-Class': 'org.test.SpringBootPersistenceApplication'
  80. )
  81. }
  82. from {
  83. configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
  84. }
  85. }
  86. }
3b6akqbq

3b6akqbq1#

排除 spring-boot-starter-web 您确实排除了它的所有依赖项,这些依赖项是在servlet环境中运行spring引导项目所必需的。最值得注意的是,你确实排除了 spring-web ,其中包含在错误消息中找到的大多数类。
顾名思义 spring-boot-starter-web 不是以tomcat为中心,而是它的依赖关系 spring-boot-starter-tomcat 是因此,您应该排除后一个工件,并包括 spring-boot-starter-undertow 要将必要的拖曳相关性拉入项目中,请执行以下操作:

  1. <parent>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-parent</artifactId>
  4. <version>2.4.5</version>
  5. </parent>
  6. ...
  7. <dependencies>
  8. ...
  9. <dependency>
  10. <groupId>org.springframework.boot</groupId>
  11. <artifactId>spring-boot-starter-web</artifactId>
  12. <exclusions>
  13. <exclusion>
  14. <groupId>org.springframework.boot</groupId>
  15. <artifactId>spring-boot-starter-tomcat</artifactId>
  16. </exclusion>
  17. </exclusions>
  18. </dependency>
  19. <dependency>
  20. <groupId>org.springframework.boot</groupId>
  21. <artifactId>spring-boot-starter-undertow</artifactId>
  22. </dependency>
  23. </dependencies>

最后一个错误可能是由于项目中存在多个不兼容的spring boot版本。要确保具有兼容的版本,请将项目的父pom设置为 spring-boot-starter-parent (或 spring-boot-dependencies )并省略指定 <version> 随时标记。SpringBoot管理许多工件的版本(请参阅托管依赖项的完整列表)。
编辑:如果使用gradle,则build.gradle应如下所示:

  1. plugins {
  2. id 'org.springframework.boot' version '2.4.5'
  3. id 'io.spring.dependency-management' version '1.0.11.RELEASE'
  4. ...
  5. }
  6. dependencies {
  7. implementation('org.springframework.boot:spring-boot-starter-web') {
  8. exclude group: 'org.springframework.boot', module: 'spring-boot-starter-tomcat'
  9. }
  10. implementation 'org.springframework.boot:spring-boot-starter-undertow'
  11. ...
  12. }
展开查看全部

相关问题