You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

VS Code中Maven Spring Boot应用启动失败求助

Spring Boot启动报错:缺失ServletWebServerFactory bean

问题描述

执行mvn clean install构建无问题,但运行mvn spring-boot:run时抛出运行时错误:

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.3.RELEASE.jar:2.3.3.RELEASE]
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:545) ~[spring-context-5.2.8.RELEASE.jar:5.2.8.RELEASE]
        at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:143) ~[spring-boot-2.3.3.RELEASE.jar:2.3.3.RELEASE]
        at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:758) ~[spring-boot-2.3.3.RELEASE.jar:2.3.3.RELEASE]

已排查pom.xml、启动类Main.java、控制器myController.java,尝试过仅保留spring-boot-starter-web依赖、给依赖添加<scope>provided</scope>,问题仍未解决。

解决方案

  • 检查启动类核心配置
    确保启动类Main.java上标注了*@SpringBootApplication*注解,且该类所在包是控制器、服务层等组件的父包,保证Spring能自动扫描到所有组件:

    @SpringBootApplication
    public class Main {
        public static void main(String[] args) {
            SpringApplication.run(Main.class, args);
        }
    }
    
  • 修正依赖配置
    确保pom.xml中正确引入spring-boot-starter-web依赖,非外部容器部署场景下不要添加<scope>provided</scope>:

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

    同时移除可能存在的冲突依赖(如手动引入的servlet-api),避免干扰Spring Boot自动配置。

  • 排查自动配置禁用情况
    确认启动类上没有通过@SpringBootApplication(exclude = ...)排除Web相关自动配置类(如ServletWebServerFactoryAutoConfiguration)。

  • 检查环境配置文件
    查看application.properties或application.yml,若存在spring.main.web-application-type=none配置,需改为spring.main.web-application-type=servlet或直接删除该配置。

  • 清理Maven缓存
    执行mvn clean install -U强制更新依赖,消除缓存损坏导致的异常。

内容的提问来源于stack exchange,提问作者user21086690

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.03 14:15:33