Spring Boot 由于缺少ReactiveWebServerFactory Bean,无法启动ReactiveWebApplicationContext

f8rj6qna  于 2022-11-23  发布在  Spring
关注(0)|答案(8)|浏览(1487)

我有一个新的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]

源代码/main/java/气泡阴影/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);
  }
}

源代码/测试/java/测试/气泡阴影/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();
  }
}
ghg1uchk

ghg1uchk1#

您的配置不足以进行React测试。
reactive WebTestClientReactiveWebApplicationContext在应用程序上下文中需要reactive服务器。
自动配置会搜索类路径,在找到React类和React上下文后,创建ReactiveWebServerFactory bean。

ego6inou

ego6inou2#

我假设您正在使用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>
hts6caw3

hts6caw33#

对我来说,这个错误是由于Spring类上缺少@SpringBootApplication注解引起的,Spring类包含实际启动 Boot 应用程序的main()方法入口点。

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

qlzsbp2j4#

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

gpfsuwkq

gpfsuwkq5#

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

x4shl7ld

x4shl7ld6#

@vdou的回答帮我解决了问题。
除了添加@EnableAutoConfiguration之外,我还必须手动添加Spring应用程序类型:

spring:
  main:
    web-application-type: reactive

很明显,我的依赖项中有一些东西导致Spring无法发现该类型。
我希望这能帮助到一些人...

kx7yvsdv

kx7yvsdv7#

如果你使用的是Kotlin,检查你的Application类中是否包含main方法,没有这个:

runApplication<Application>{
    webApplicationType = WebApplicationType.REACTIVE
}

然后把“REACTIVE”改成“SERVELET”,就会工作得很有魅力。

0yg35tkg

0yg35tkg8#

如果以上解决方案都不起作用,请尝试添加

@ContextConfiguration(loader = AnnotationConfigContextLoader.class)

它可能会帮助你

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

相关问题