我试图用 Spring Boot 2.06 构建一个反应式 Web 应用程序并得到这个错误:
org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is org.springframework.context.ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean.
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:155) ~[spring-boot-2.0.6.RELEASE.jar:2.0.6.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:542) ~[spring-context-5.0.10.RELEASE.jar:5.0.10.RELEASE]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140) ~[spring-boot-2.0.6.RELEASE.jar:2.0.6.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:754) [spring-boot-2.0.6.RELEASE.jar:2.0.6.RELEASE]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:386) [spring-boot-2.0.6.RELEASE.jar:2.0.6.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:307) [spring-boot-2.0.6.RELEASE.jar:2.0.6.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1242) [spring-boot-2.0.6.RELEASE.jar:2.0.6.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1230) [spring-boot-2.0.6.RELEASE.jar:2.0.6.RELEASE]
at com.vogella.springboot2.Test3Application.main(Test3Application.java:10) [main/:na]
Caused by: org.springframework.context.ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean.
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.getWebServerFactory(ServletWebServerApplicationContext.java:204) ~[spring-boot-2.0.6.RELEASE.jar:2.0.6.RELEASE]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.createWebServer(ServletWebServerApplicationContext.java:178) ~[spring-boot-2.0.6.RELEASE.jar:2.0.6.RELEASE]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:152) ~[spring-boot-2.0.6.RELEASE.jar:2.0.6.RELEASE]
... 8 common frames omitted
这个链接解释了这个问题,并提供了两个建议:
https://vividcode.io/Fixing-Spring-Boot-error-Unable-to-start-ServletWebServerApplicationContext-due-to-missing-ServletWebServerFactory-bean/
我正在使用 Spring Boot 构建一个新的 Spring WebFlux 应用程序。从start.spring.io下载项目模板后,我添加了一些第三方依赖并尝试启动应用程序。然后我遇到了错误 org.springframework.context.ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean....
错误消息是解决方案的线索。它说无法启动 ServletWebServerApplicationContext,但我的项目使用的是 WebFlux,它是一个响应式 Web 项目,而不是基于 servlet 的 Spring Web MVC 项目。
调试 Spring 源代码帮助我找到了原因。在 org.springframework.boot.SpringApplication 的方法 deuceWebApplicationType 中,只有当 CLASSPATH 中不存在 Spring Web MVC 的类时,Web 应用程序类型才设置为 WebApplicationType.REACTIVE。
一旦确定了根本原因,解决方案就很容易了。我们可以:
更新 Maven 依赖项以排除 spring-webmvc,或者将 web 应用程序类型显式设置为 WebApplicationType.REACTIVE,如下所示。
@SpringBootApplication class MyApplication
fun main(args: Array<String>) {
val app = SpringApplication(MyApplication::class.java)
app.webApplicationType = WebApplicationType.REACTIVE
app.run(*args)
}
不幸的是,这些解决方案都不适合我。相反,我调整了 geoand 的响应,并将其添加到我的application.properties
:
spring.main.web-application-type=reactive
瞧!问题解决了!