欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

在Spring容器中解决找不到ServletWebServerFactory类引发的异常问题

最编程 2024-02-07 22:06:48
...

【已解决】Spring容器中找不到ServletWebServerFactory类出现的异常

【已解决】ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean异常

概述

最近在运行一个springboot项目时,出现了下面的异常。

异常情况

具体异常如下所示

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: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.2.10.RELEASE.jar:5.2.10.RELEASE]
	at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.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(PrimoDubboSwaggerTestServerApplication.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(ServletWebServerApplicationContext.java:205) ~[spring-boot-2.3.5.RELEASE.jar:2.3.5.RELEASE]
	at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.createWebServer(ServletWebServerApplicationContext.java:177) ~[spring-boot-2.3.5.RELEASE.jar:2.3.5.RELEASE]
	at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:158) ~[spring-boot-2.3.5.RELEASE.jar:2.3.5.RELEASE]
	... 9 common frames omitted
08175839_A0EecY
08175839_A0EecY

异常分析

这个异常情况有很多种,实际就是缺失了需要的类没有找到。

也就是在容器中没有注入ServletWebServerFactory类。

解决方案

这个异常可能有多个解决方案,因为有很多的原因。我先说说我用于解决问题的方案。

解决方案一

我是通过添加一个配置进行解决的。

在application.properties中可以进行添加:

spring.main.web-application-type=none

如果是yml文件,也就是 application.yml,可以添加:

  spring:
    main:
      web-application-type: none

这种处理方式可以在项目不是web项目的时候配置。

解决方案二

如果是springboot ,并且是web项目。那么检查一下是不是添加了@SpringBootApplication注解。

没有的话,加好就行了。

解决方案三

最后,可能就是缺失那个类了。检查一下依赖。

是不是有spring-boot-starter-web依赖了。

如果没有,那么在pom.xml文件中加好配置。

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

如果项目中用到了spring-boot-starter-webflux,也是使用的这种解决方案。