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

【已解决】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

异常分析

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

也就是在容器中没有注入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,也是使用的这种解决方案。

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

谙忆

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值