This article is participating in the Java Theme Month – Java Debug Notes Event, see the event link for details

Resolved ApplicationContextException 】 : Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean exception

An overview of the

The following exception recently occurred while running a Springboot project.

Abnormal situation

The specific exceptions are as follows

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(ServletWebServerApplicationCon text.java:161) ~[spring-boot-2.3. 5.RELEASE.jar:2.3. 5.RELEASE]
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:545) ~[spring-context-5.210..RELEASE.jar:5.210..RELEASE] at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationConte xt.java:143) ~[spring-boot-2.3. 5.RELEASE.jar:2.3. 5.RELEASE]
	at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:758) [spring-boot-2.3. 5.RELEASE.jar:2.3. 5.RELEASE]
	at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:750) [spring-boot-2.3. 5.RELEASE.jar:2.3. 5.RELEASE]
	at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:405) [spring-boot-2.3. 5.RELEASE.jar:2.3. 5.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) [spring-boot-2.3. 5.RELEASE.jar:2.3. 5.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1237) [spring-boot-2.3. 5.RELEASE.jar:2.3. 5.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226) [spring-boot-2.3. 5.RELEASE.jar:2.3. 5.RELEASE] at wiki.primo.dubbo.swagger.test.server.primodubboswaggertestserver.PrimoDubboSwaggerTestServerApplication.main(PrimoDubboS waggerTestServerApplication.java:12) [classes/: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(ServletWebServerAppl icationContext.java:205) ~[spring-boot-2.3. 5.RELEASE.jar:2.3. 5.RELEASE] at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.createWebServer(ServletWebServerApplicat ionContext.java:177) ~[spring-boot-2.3. 5.RELEASE.jar:2.3. 5.RELEASE] at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationCon text.java:158) ~[spring-boot-2.3. 5.RELEASE.jar:2.3. 5.RELEASE]
	... 9 common frames omitted
Copy the code

Abnormal analysis

There are many different cases of this exception, where the required class is missing and not found.

That is, the ServletWebServerFactory class is not injected into the container.

The solution

There can be multiple solutions to this exception, for many reasons. Let me start with my solution to the problem.

Solution 1

I solved this by adding a configuration.

In application.properties you can add:

spring.main.web-application-type=none
Copy the code

If it is a YML file, i.e. Application.yml, you can add:

  spring:
    main:
      web-application-type: none
Copy the code

This processing can be configured when the project is not a Web project.

Solution 2

If it’s SpringBoot and it’s a Web project. So check to see if the @SpringBootApplication annotation is added.

If not, just add it.

Solution 3

In the end, it might be the missing class. Check dependencies.

Is there a spring-boot-starter-web dependency?

If not, add the configuration to the POM.xml file.

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-web</artifactId>
</dependency>
Copy the code

This solution is also used if spring-boot-starter-webflux is used in the project.