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

Dan*_*nch 13 java spring-boot spring-boot-test

我有一个新的springboot应用程序,我试图开始.

我收到的错误是

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]
Run Code Online (Sandbox Code Playgroud)

的src /主/爪哇/ 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);
  }
}
Run Code Online (Sandbox Code Playgroud)

的src /测试/ JAVA /测试/ 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();
  }
}
Run Code Online (Sandbox Code Playgroud)

Tim*_*sen 11

@SpringBootApplication对我来说,该错误是由于Spring 类缺少注释引起的,main()该类包含实际启动 Boot 应用程序的方法入口点。使用以下解决了错误:

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


Fra*_*ire 9

我假设您正在使用maven来获取您的依赖项.

我用以下方法解决了这个问题:

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-webflux</artifactId>
        <version>2.0.3.RELEASE</version>
    </dependency>
Run Code Online (Sandbox Code Playgroud)

代替:

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webflux</artifactId>
        <version>5.0.7.RELEASE</version>
    </dependency>
Run Code Online (Sandbox Code Playgroud)


vdo*_*dou 6

您的配置不足以进行反应性测试。

在应用程序上下文中的反应式WebTestClientReactiveWebApplicationContext需要反应式服务器。@EnableAutoConfiguration向您添加注释,RootControllerTest然后让Spring为您完成。

自动配置会搜索您的类路径,并在找到反应性类和反应性上下文之后创建ReactiveWebServerFactorybean。

  • 我使用的是 Spring Boot 版本 2.2.5.RELEASE,并且我还必须将这些类显式添加到“@SpringBootTest”注释中(即使它们分别用“@Configuration”和“@Component”注释):“@ SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, 类 = {FooConfig.class, FooHandler.class})` (2认同)

Ani*_*rni 5

可能是一个腐败的下载.尝试删除〜/ .m2/respository.