Spring 启动 Webflux 应用程序作为应用程序运行良好,但未能通过 springboottest 尝试创建 servlet 容器上下文
Spring boot Webflux application runs fine as an app, but fails springboottest trying to create a servlet container context
该项目正在从 spring 和球衣的混合迁移到完整的 spring 启动 webflux
实际的错误信息是:
Type org.glassfish.jersey.servlet.ServletContainer not present
问题是我不知道为什么测试试图生成这个上下文,而且调试非常困难。
测试声明为:
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.web.reactive.server.WebTestClient;
import org.springframework.web.reactive.function.client.ExchangeStrategies;
import org.springframework.web.reactive.function.client.WebClient;
@ExtendWith(SpringExtension.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@AutoConfigureWebTestClient
@TestPropertySource(properties = {"opentracing.sample-rate=1"})
class ReactiveControllerIT {
@LocalServerPort
int port;
@Autowired
private WebTestClient webClient;
以及申请class
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.neo4j.repository.config.EnableReactiveNeo4jRepositories;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
@SpringBootApplication
@EnableScheduling
@EnableReactiveNeo4jRepositories
@EnableGlobalMethodSecurity(prePostEnabled = true, proxyTargetClass = true)
public class SeoGraphApplication {
public static void main(String[] args) {
SpringApplication.run(SeoGraphApplication.class, args);
}
}
即使使用空的测试方法,我也没有达到实际的方法。
有什么想法吗?
谢谢,
奥马尔。
找到了,调试了好几个小时。
这个方法是解决问题的关键:
private WebApplicationType deduceWebApplicationType() {
if (ClassUtils.isPresent(REACTIVE_WEB_ENVIRONMENT_CLASS, null)
&& !ClassUtils.isPresent(MVC_WEB_ENVIRONMENT_CLASS, null)
&& !ClassUtils.isPresent(JERSEY_WEB_ENVIRONMENT_CLASS, null)) {
return WebApplicationType.REACTIVE;
}
for (String className : WEB_ENVIRONMENT_CLASSES) {
if (!ClassUtils.isPresent(className, null)) {
return WebApplicationType.NONE;
}
}
return WebApplicationType.SERVLET;
}
关于 Jersey web 环境的 if 结果为 true,这最终使该方法成为 return servlet 类型。
它的来源是我公司错误类型库的使用,出于某种奇怪的原因,它依赖于 jersey-server,当找到时,会导致 servlet 上下文初始化。
我就此联系了作者,最糟糕的是排除服务器依赖性对功能没有影响。
感谢所有为此付出时间的人。
该项目正在从 spring 和球衣的混合迁移到完整的 spring 启动 webflux
实际的错误信息是:
Type org.glassfish.jersey.servlet.ServletContainer not present
问题是我不知道为什么测试试图生成这个上下文,而且调试非常困难。
测试声明为:
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.web.reactive.server.WebTestClient;
import org.springframework.web.reactive.function.client.ExchangeStrategies;
import org.springframework.web.reactive.function.client.WebClient;
@ExtendWith(SpringExtension.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@AutoConfigureWebTestClient
@TestPropertySource(properties = {"opentracing.sample-rate=1"})
class ReactiveControllerIT {
@LocalServerPort
int port;
@Autowired
private WebTestClient webClient;
以及申请class
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.neo4j.repository.config.EnableReactiveNeo4jRepositories;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
@SpringBootApplication
@EnableScheduling
@EnableReactiveNeo4jRepositories
@EnableGlobalMethodSecurity(prePostEnabled = true, proxyTargetClass = true)
public class SeoGraphApplication {
public static void main(String[] args) {
SpringApplication.run(SeoGraphApplication.class, args);
}
}
即使使用空的测试方法,我也没有达到实际的方法。
有什么想法吗?
谢谢,
奥马尔。
找到了,调试了好几个小时。
这个方法是解决问题的关键:
private WebApplicationType deduceWebApplicationType() {
if (ClassUtils.isPresent(REACTIVE_WEB_ENVIRONMENT_CLASS, null)
&& !ClassUtils.isPresent(MVC_WEB_ENVIRONMENT_CLASS, null)
&& !ClassUtils.isPresent(JERSEY_WEB_ENVIRONMENT_CLASS, null)) {
return WebApplicationType.REACTIVE;
}
for (String className : WEB_ENVIRONMENT_CLASSES) {
if (!ClassUtils.isPresent(className, null)) {
return WebApplicationType.NONE;
}
}
return WebApplicationType.SERVLET;
}
关于 Jersey web 环境的 if 结果为 true,这最终使该方法成为 return servlet 类型。
它的来源是我公司错误类型库的使用,出于某种奇怪的原因,它依赖于 jersey-server,当找到时,会导致 servlet 上下文初始化。 我就此联系了作者,最糟糕的是排除服务器依赖性对功能没有影响。
感谢所有为此付出时间的人。