前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【已解决】Spring容器中找不到ServletWebServerFactory类出现的异常

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

作者头像
谙忆
发布2021-10-26 11:20:06
13K0
发布2021-10-26 11:20:06
举报
文章被收录于专栏:程序编程之旅程序编程之旅

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

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

概述

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

异常情况

具体异常如下所示

代码语言:javascript
复制
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中可以进行添加:

代码语言:javascript
复制
spring.main.web-application-type=none

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

代码语言:javascript
复制
  spring:
    main:
      web-application-type: none

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

解决方案二

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

没有的话,加好就行了。

解决方案三

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

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

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

代码语言:javascript
复制
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-web</artifactId>
</dependency>

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

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2021-05-16 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 概述
  • 异常情况
  • 异常分析
  • 解决方案
    • 解决方案一
      • 解决方案二
        • 解决方案三
        相关产品与服务
        容器服务
        腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档