I was trying to build a Reactive Web App with Spring Boot 2.06 and got this error:
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
This link explained the problem, and offered two suggestions:
https://vividcode.io/Fixing-Spring-Boot-error-Unable-to-start-ServletWebServerApplicationContext-due-to-missing-ServletWebServerFactory-bean/
I was building a new Spring WebFlux application with Spring Boot.
After downloading the project template from start.spring.io, I added
some third-party dependencies and tried to start the application. Then
I met the error
org.springframework.context.ApplicationContextException: Unable to
start ServletWebServerApplicationContext due to missing
ServletWebServerFactory bean....
The error message is the clue to the solution. It says Unable to start
ServletWebServerApplicationContext, but my project is using WebFlux
and it’s a reactive web project, not a servlet-based Spring Web MVC
project.
Debugging into the Spring source code helped me find the cause. In the
method deduceWebApplicationType of
org.springframework.boot.SpringApplication, the web application type
is set to WebApplicationType.REACTIVE only when classes of Spring Web
MVC is not present in the CLASSPATH.
The solution is easy once the root cause is identified. We can either:
Update Maven dependencies to exclude spring-webmvc, or Set the web
application type to WebApplicationType.REACTIVE explicitly, as shown
below.
@SpringBootApplication class MyApplication
fun main(args: Array<String>) {
val app = SpringApplication(MyApplication::class.java)
app.webApplicationType = WebApplicationType.REACTIVE
app.run(*args)
}
Unfortunately, neither of these solutions worked for me. Instead, I adapted geoand's response, and added this to my application.properties
:
spring.main.web-application-type=reactive
Voila! Problem resolved!