美文网首页
Unable to start ServletWebServer

Unable to start ServletWebServer

作者: 半寿翁 | 来源:发表于2023-01-22 21:37 被阅读0次

1 报错描述

一个android开发,学习一些东西需要服务端的内容配合,不好麻烦同事,只能自己研究一下,结果打jar测试一下就给我来了个大惊喜。。。

 ERROR org.springframework.boot.SpringApplication - Application run failed
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:163)
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:577)
        at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:145)
        at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:730)
        at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:412)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:302)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1301)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1290)
        at com.bsw.AppApplication.main(AppApplication.java:21)
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:210)
        at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.createWebServer(ServletWebServerApplicationContext.java:180)
        at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:160)
        ... 8 common frames omitted

我在开发调试的时候都没事,结果运行jar包的时候出问题了,网上查了N多方案,最后总算找到了解决方案,只是这个方案的搜索结果太少了,特此记录一下,避免以后有问题的时候还得花这么长时间去找。


机智如我

方案来自【spring boot】 IDEA 启动springboot项目报missing ServletWebServerFactory,特此鸣谢!

特此鸣谢

2 我项目中生效的解决方案

其实很简单,报错中只有最重要的一句话就是:

 Unable to start web server; nested exception is org.springframework.context.ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean.

翻译一下就是因为缺少ServletWebServerFactory导致ServletWebServerApplicationContext启动失败,作为一个实用主义菜鸟,这些都是拿来做什么的不重要,能用就行,所以却ServletWebServerFactory那就补ServletWebServerFactory呗。

没错,只需要在启动类中添加如下代码即可:

@Bean
ServletWebServerFactory servletWebServerFactory(){
   return new TomcatServletWebServerFactory();
}

例如我的项目就是:

@SpringBootApplication
@ComponentScan
public class AppApplication implements CommandLineRunner {

    public static void main(String[] args) {
        SpringApplication.run(AppApplication.class, args);
    }

    @Bean
    ServletWebServerFactory servletWebServerFactory(){
        return new TomcatServletWebServerFactory();
    }
}

这样上面的问题就修复了,是不是相当简单。

3 其他解决方案

虽然我项目中没有出现下述方案对应的问题,但是有备无患,我这里也记录一下,日后万一遇到问题,手里有个相对完整的解决方案归纳,总比重新网上查要省事一些。

如下方案来自【已解决】Spring容器中找不到ServletWebServerFactory类出现的异常,特此鸣谢

3.1 添加一个配置

如果使用的是application.properties,则添加:

spring.main.web-application-type=none

如果使用的是application.yml,则添加:

  spring:
    main:
      web-application-type: none

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

3.2 检查启动类配置

检查在项目启动类上是否添加了@SpringBootApplication注解(该问题一般情况下不会出现,因为在创建SpringBoot的时候会自动添加,除非是修改的时候误删了)

3.3 缺少spring-boot-starter-web依赖

缺啥补啥,是maven的还是gradle的按照自己的项目情况配置就可以。
如果想找最新版本可以取如下链接查找:
Spring Boot Starter Web

版本选择 gradle maven

4. 搞定

总算写完了,作为一个菜鸟,虽然解决了这个问题,可项目运行还有其他问题,接着苦逼地修bug去了。。。


泪如潮水

相关文章

网友评论

      本文标题:Unable to start ServletWebServer

      本文链接:https://www.haomeiwen.com/subject/pazkhdtx.html