【问题标题】:Unable to start ReactiveWebApplicationContext due to missing ReactiveWebServerFactory bean由于缺少 ReactiveWebServerFactory bean,无法启动 ReactiveWebApplicationContext
【发布时间】:2018-10-24 02:01:15
【问题描述】:

我正在尝试启动一个新的 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]

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();
  }
}

【问题讨论】:

    标签: java spring-boot spring-boot-test


    【解决方案1】:

    我假设你正在使用 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 ?
    • 我可以通过将@ComponentScan 添加到应用程序或配置来解决这个问题
    【解决方案2】:

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

    响应式WebTestClientReactiveWebApplicationContext 在应用程序上下文中需要响应式服务器。将注解 @EnableAutoConfiguration 添加到您的 RootControllerTest 并让 Spring 为您完成。

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

    【讨论】:

    • 我正在使用 Spring Boot 版本 2.2.5.RELEASE,并且我还必须将这些类显式添加到 @SpringBootTest 注释中(即使它们使用 @Configuration@Component 进行了注释)分别):@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = {FooConfig.class, FooHandler.class})
    • 我想也许带有适当包的 @ComponentScan 注释也可以解决问题。
    【解决方案3】:

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

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

    【讨论】:

      【解决方案4】:

      可能是损坏的下载。尝试删除 ~/.m2/repository。

      【讨论】:

        【解决方案5】:

        实际上,您只需将 @SpringBootTest 注释中的 webEnvironment = WebEnvironment.RANDOM_PORT 更改为 webEnvironment = WebEnvironment.MOCK

        【讨论】:

          猜你喜欢
          • 2014-03-14
          • 2015-03-19
          • 2018-10-18
          • 2016-05-06
          • 1970-01-01
          • 2019-08-07
          • 2016-03-05
          • 2014-07-28
          • 1970-01-01
          相关资源
          最近更新 更多