Spring Reactive Test case 启动Netty Server失败

Spring Reactive Test case fails to start the Netty Server

我有 spring 测试用例,如下所示,当我 运行 它没有启动 Netty 服务器并提供以下异常时。

Caused by: org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is org.springframework.context.ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean.
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:155)

下面是我的测试用例:

@RunWith(SpringRunner.class)
@SpringBootTest(classes = SpringWebFluxDemoApplication.class, webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
public class CustomPriceControllerTest {

    @Autowired
    private ApplicationContext context;

    private WebTestClient testClient;

    @Before
    public void init() {
        testClient = WebTestClient.bindToApplicationContext(context).configureClient().baseUrl("http://localhost:8080").responseTimeout(Duration.ofSeconds(30)).build();
    }

    @Test
    public void broadcastVoltageConsumption() {

        this.testClient.get().uri("/api/getCustomPrice")
                .accept(MediaType.APPLICATION_STREAM_JSON)
                .exchange()
                .expectBodyList(Custom.class)
                .consumeWith((customResults) -> {
                    List<Custom> list = CustomResults.getResponseBody();
                    Assert.assertTrue(list != null && list.size() > 0);
                });
    }
}

我的 pom.xml 已排除 tomcat 的依赖项以启用 Netty。我的 Spring 启动 class 工作得很好。它启动 Netty 服务器。

更新 - pom.xml

 <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-rest</artifactId>
            <exclusions>
                <!-- Exclude the Tomcat dependency -->
                <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-webflux</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>io.projectreactor</groupId>
            <artifactId>reactor-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

必须添加 javax.servlet-api,因为我遇到 javax.servlet-api 缺失的问题。

更新 - 2 从 pom.xml 中删除 javax.servlet 依赖项可以解决问题。

而当我尝试 运行 我的主应用程序时,它会正常启动 Netty 服务器。此配置中缺少什么?有人可以帮忙吗?

您希望网络服务器由 Netty 提供支持,但您遇到的异常是特定于 Servlet 的

由于 Netty 不是基于 servlet 的技术,您似乎在某处 (gradle/maven/@Configuration) 混合了它们, 所以,只需删除所有对 Servlet 依赖项的引用,然后重新尝试

我遇到了同样的问题。我只是删除了这个依赖项(它有 servlet 依赖项):

testCompile('org.springframework.restdocs:spring-restdocs-mockmvc')

如果任何依赖项需要一个 servlet,它会强制启动 Jetty,在这种情况下 netty 永远不会启动。

每当您看到 webflux 正在启动 Jetty 而不是 netty 时,您应该 运行 按照命令然后搜索谁将 Jetty 作为依赖项,然后您会找到答案。

./gradlew dependencies

在您的情况下,您直接包含了导致该问题的 servlet 依赖项。

    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <scope>test</scope>
    </dependency>