gpt4 book ai didi

unit-testing - 如何在测试时使 Spring Boot webflux 在默认 Netty 而不是 Jetty 上启动

转载 作者:行者123 更新时间:2023-12-04 15:43:44 27 4
gpt4 key购买 nike

我有一个有点类似的 gradle 和 spring webflux 配置,如 Why spring webflux is choosing jetty by default and then failing? 中所述。

但是我们的目标是在内存中使用 Amazon dynamo db + spring webflux on netty(而不是 jetty)来进行单元测试(我们已经在 netty 上拥有带有 spring webfux 的生产 dynamo db)。我能够使用内存中的 dynamo db 运行单元测试,但是一旦我启用 springboot webflux,它就会开始提示:

   Caused by: java.lang.NoClassDefFoundError: org/eclipse/jetty/servlet/ServletHolder
at org.springframework.boot.web.embedded.jetty.JettyReactiveWebServerFactory.createJettyServer(JettyReactiveWebServerFactory.java:176) ~[spring-boot-2.1.5.RELEASE.jar:2.1.5.RELEASE]
at org.springframework.boot.web.embedded.jetty.JettyReactiveWebServerFactory.getWebServer(JettyReactiveWebServerFactory.java:106) ~[spring-boot-2.1.5.RELEASE.jar:2.1.5.RELEASE]
at org.springframework.boot.web.reactive.context.ReactiveWebServerApplicationContext$ServerManager.<init>(ReactiveWebServerApplicationContext.java:202) ~[spring-boot-2.1.5.RELEASE.jar:2.1.5.RELEASE]
at org.springframework.boot.web.reactive.context.ReactiveWebServerApplicationContext$ServerManager.get(ReactiveWebServerApplicationContext.java:221) ~[spring-boot-2.1.5.RELEASE.jar:2.1.5.RELEASE]
at org.springframework.boot.web.reactive.context.ReactiveWebServerApplicationContext.createWebServer(ReactiveWebServerApplicationContext.java:90) ~[spring-boot-2.1.5.RELEASE.jar:2.1.5.RELEASE]
at org.springframework.boot.web.reactive.context.ReactiveWebServerApplicationContext.onRefresh(ReactiveWebServerApplicationContext.java:79) ~[spring-boot-2.1.5.RELEASE.jar:2.1.5.RELEASE]
... 62 common frames omitted
Caused by: java.lang.ClassNotFoundException: org.eclipse.jetty.servlet.ServletHolder

我在 build.gradle 中尝试过以下操作:
 configurations {
// exclude Reactor Jetty /Tomcat
compile.exclude group: 'org.springframework.boot',module: 'spring-boot-starter-jetty'
testcompile.exclude group: 'org.springframework.boot',module: 'spring-boot-starter-jetty'
//compile.exclude group: 'javax.servlet' , module: 'servlet-api' //<--tried servlet jar exclusion also
}

.....
testCompile ('com.amazonaws:DynamoDBLocal:1.11.477')
....//event tried this ( and is probably wrong)
testCompile("org.springframework.boot:spring-boot-starter-test:$springBootVersion") {
exclude group: 'org.springframework.boot', module: 'spring-boot-starter-jetty' //by both name and group
exclude group: 'org.springframework.boot', module: 'spring-boot-starter-tomcat'

}

我检查了依赖关系图:
\--- com.amazonaws:DynamoDBLocal:1.11.477
+--- org.antlr:antlr4-runtime:4.7.2
+--- commons-cli:commons-cli:1.2
+--- org.apache.commons:commons-lang3:3.8.1
+--- com.almworks.sqlite4java:libsqlite4java-linux-i386:1.0.392
| \--- com.almworks.sqlite4java:sqlite4java:1.0.392
+--- com.almworks.sqlite4java:libsqlite4java-linux-amd64:1.0.392
| \--- com.almworks.sqlite4java:sqlite4java:1.0.392
+--- com.almworks.sqlite4java:sqlite4java-win32-x64:1.0.392
| \--- com.almworks.sqlite4java:sqlite4java:1.0.392
+--- com.almworks.sqlite4java:sqlite4java-win32-x86:1.0.392
| \--- com.almworks.sqlite4java:sqlite4java:1.0.392
+--- com.almworks.sqlite4java:libsqlite4java-osx:1.0.392
| \--- com.almworks.sqlite4java:sqlite4java:1.0.392
+--- com.amazonaws:aws-java-sdk-core:1.11.477 (*)
+--- com.amazonaws:aws-java-sdk-dynamodb:1.11.477 (*)
+--- org.apache.logging.log4j:log4j-api:2.6.2 -> 2.11.2
+--- org.apache.logging.log4j:log4j-core:2.6.2 -> 2.11.2
| \--- org.apache.logging.log4j:log4j-api:2.11.2
+--- org.eclipse.jetty:jetty-client:8.1.12.v20130726 -> 9.4.18.v20190429
| +--- org.eclipse.jetty:jetty-http:9.4.18.v20190429
| | +--- org.eclipse.jetty:jetty-util:9.4.18.v20190429
| | \--- org.eclipse.jetty:jetty-io:9.4.18.v20190429
| | \--- org.eclipse.jetty:jetty-util:9.4.18.v20190429
| \--- org.eclipse.jetty:jetty-io:9.4.18.v20190429 (*)
+--- org.eclipse.jetty:jetty-server:8.1.12.v20130726 -> 9.4.18.v20190429
| +--- javax.servlet:javax.servlet-api:3.1.0 -> 4.0.1
| +--- org.eclipse.jetty:jetty-http:9.4.18.v20190429 (*)
| \--- org.eclipse.jetty:jetty-io:9.4.18.v20190429 (*)
+--- org.mockito:mockito-core:1.10.19 -> 2.23.4
| +--- net.bytebuddy:byte-buddy:1.9.3 -> 1.9.12


所以我需要的是强制 webflux 将 Netty 作为服务器。我认为它正在被 dynamodb 依赖或一些 jar 传递性覆盖。

最佳答案

我遇到了这个完全相同的问题。问题在于DynamoDBLocal库可传递地引用 Jetty。由于 Jetty 和 Netty 都在类路径上,Webflux 认为它应该使用 Jetty 而不是 Netty 来启动。我能够按照此处 Spring Boot 文档中的详细说明解决此问题:https://docs.spring.io/spring-boot/docs/current/reference/html/howto-embedded-web-servers.html#howto-configure-webserver

具体这部分:

As a last resort, you can also declare your own WebServerFactory component, which will override the one provided by Spring Boot.



更具体地说,您可以将其添加到您的测试配置(或您的整个应用程序配置,如果您想明确告诉所有内容始终使用 Netty):
@Bean
public ReactiveWebServerFactory reactiveWebServerFactory() {
return new NettyReactiveWebServerFactory();
}

关于unit-testing - 如何在测试时使 Spring Boot webflux 在默认 Netty 而不是 Jetty 上启动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56783116/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com