Spring Reactive Test case 启动 Netty Server 失败

Bil*_*ins 5 java spring spring-boot spring-webflux

当我运行它时,我有如下所示的 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)
Run Code Online (Sandbox Code Playgroud)

下面是我的测试用例:

@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);
                });
    }
}
Run Code Online (Sandbox Code Playgroud)

我的 pom.xml 已经排除了 tomcat 的依赖来启用 Netty。我的 Spring 引导类工作得很好。它启动 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>
Run Code Online (Sandbox Code Playgroud)

不得不添加 javax.servlet-api 因为我遇到了缺少 javax.servlet-api 的问题。

更新 - 2 从 pom.xml 中删除 javax.servlet 依赖解决了这个问题。

当我尝试运行主应用程序时,它会正常启动 Netty 服务器。这个配置缺少什么?任何人都可以帮忙吗?

vam*_*egi 4

您希望 Web 服务器由 Netty 提供支持,但您遇到的例外是 Servlet Specific

由于 Netty 不是基于 servlet 的技术,看来您在某个地方(gradle/maven/@Configuration)混合它们,因此,只需删除对 Servlet 依赖项的所有引用并重试