Java 由于缺少ReactiveWebServerFactory bean,无法启动ReactiveWebApplicationContext
我有一个新的springboot应用程序,我正在尝试开始 我收到的错误是Java 由于缺少ReactiveWebServerFactory bean,无法启动ReactiveWebApplicationContext,java,spring-boot,spring-boot-test,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 ReactiveWebApplicationCont
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);
}
}
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();
}
}
我假设您正在使用maven获取依赖项 我使用以下方法解决了问题:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
<version>2.0.3.RELEASE</version>
</dependency>
org.springframework.boot
弹簧启动器webflux
2.0.3.1发布
而不是:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webflux</artifactId>
<version>5.0.7.RELEASE</version>
</dependency>
org.springframework
春季网络流量
5.0.7.发布
下载可能已损坏。尝试删除~/.m2/存储库。您的配置不足以进行反应式测试
反应式WebTestClient
以及ReactiveWebApplicationContext
需要应用程序上下文中的反应式服务器。将注释@EnableAutoConfiguration
添加到RootControllerTest
中,让Spring为您完成
自动配置会搜索您的类路径,在找到反应类和反应上下文后,再创建
ReactiveWebServerFactory
bean。对我来说,错误是由包含main()的Spring类上缺少@SpringBootApplication
注释引起的
实际启动启动应用程序的方法入口点。使用以下命令解决了此错误:
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
实际上,您只需要在
@springbootest
注释中将webEnvironment=webEnvironment.RANDOM\u PORT
更改为webEnvironment=webEnvironment.MOCK
。您尝试过一个小工具解决方案吗?删除~/.m2/存储库?我可以通过向应用程序或配置添加@ComponentScan来解决这个问题。我使用的是Spring Boot版本2.2.5.RELEASE,我还必须将类显式添加到@SpringBootTest
注释中(即使它们分别用@Configuration
和@Component
注释):@springbootest(webEnvironment=springbootest.webEnvironment.RANDOM_PORT,class={FooConfig.class,foodhandler.class})
我想可能是一个带有适当软件包的@ComponentScan
注释也能起到作用。