由于缺少ReactiveWebServerFactory bean,无法启动ReactiveWebApplicationContext。

28

我有一个新的Spring Boot应用程序,我正在尝试启动它。

我收到的错误信息是:

org.springframework.context.ApplicationContextException: Unable to start reactive web server; nested exception is org.springframework.context.ApplicationContextException: Unable to start ReactiveWebApplicationContext due to missing ReactiveWebServerFactory bean.
    at org.springframework.boot.web.reactive.context.ReactiveWebServerApplicationContext.onRefresh(ReactiveWebServerApplicationContext.java:76) ~[spring-boot-2.0.1.RELEASE.jar:2.0.1.RELEASE]

src/main/java/bubbleshadow/RootController.java

package bubbleshadow;

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Mono;

@RestController
public class RootController {
  public RootController() {

  }

  @GetMapping("/")
  public Mono<HttpStatus> returnOk() {
    return Mono.just(HttpStatus.OK);
  }
}

src/test/java/test/bubbleshadow/RootControllerTest.java

package test.bubbleshadow;
import bubbleshadow.RootController;

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.WebFluxTest;
import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient;
import org.springframework.test.web.reactive.server.WebTestClient;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;

@ExtendWith(SpringExtension.class)
@SpringBootTest(classes=RootController.class, webEnvironment = WebEnvironment.RANDOM_PORT)
@AutoConfigureWebTestClient
public class RootControllerTest {
  @Autowired
  WebTestClient webTestClient;

  @Test
  public void baseRouteShouldReturnStatusOK() {
    webTestClient.head().uri("/").exchange().expectStatus().isOk();
  }
}
9个回答

18

您的配置不足以进行响应式测试。

响应式WebTestClientReactiveWebApplicationContext需要在应用程序上下文中具有响应式服务器。在RootControllerTest上添加注释@EnableAutoConfiguration,让Spring为您完成。

自动配置将搜索您的类路径,并在找到响应式类和响应式上下文后创建ReactiveWebServerFactory bean。


2
我正在使用Spring Boot版本2.2.5.RELEASE,并且我还必须将这些类显式地添加到@SpringBootTest注释中(即使它们分别带有@Configuration@Component注释):@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = {FooConfig.class, FooHandler.class}) - user2337270
我猜可能使用带有适当包的@ComponentScan注释也可以解决问题。 - user2337270

15

我假设你正在使用Maven获取你的依赖。

我通过使用以下方式解决了这个问题:

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-webflux</artifactId>
        <version>2.0.3.RELEASE</version>
    </dependency>

而不是:

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webflux</artifactId>
        <version>5.0.7.RELEASE</version>
    </dependency>

你尝试过Aniket的解决方案吗?删除你的~/.m2/repository目录了吗? - François Dupire
我通过在应用程序或配置中添加@ComponentScan来解决了这个问题。 - Stephan

11

对我而言,该错误是由于Spring类中缺少@SpringBootApplication注解造成的。而main()方法入口实际上是启动Boot应用程序的。使用以下内容解决了此错误:

@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

4
很可能是下载损坏了。请尝试删除~/.m2/repository。

4
你只需要在@SpringBootTest注解中将webEnvironment = WebEnvironment.RANDOM_PORT改为webEnvironment = WebEnvironment.MOCK即可。

1

@vdou的回答帮助我解决了我的问题。

除了添加@EnableAutoConfiguration之外,我还必须手动添加Spring应用程序类型:

spring:
  main:
    web-application-type: reactive

显然我的依赖关系中有些东西导致Spring无法发现该类型。

希望这能帮助到某些人...


0
如果您正在使用Kotlin,请检查包含主方法的Application类是否没有以下内容:
runApplication<Application>{
    webApplicationType = WebApplicationType.REACTIVE
}

然后将“REACTIVE”更改为“SERVLET”,就可以完美运行。

0
如果以上解决方案都不起作用,请尝试添加。
@ContextConfiguration(loader = AnnotationConfigContextLoader.class)

这可能会对你有所帮助

import org.springframework.test.context.support.AnnotationConfigContextLoader;

0
另一个可能导致这种情况发生的原因是,如果您正在为测试导入一个未标记有 @TestConfiguration 注释的配置类。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接