【Java】Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean

本文目录

一、启动类忘记添加注解

二、缺少依赖,添加即可

三、启动类main方法写错

四、移除排除项

五、我的解决方案


报错信息如下:Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean

在Spring Boot项目中,出现这个错误有几种情况:

一、启动类忘记添加注解

在main方法所在的类忘记添加@SpringBootApplication;

二、缺少依赖,添加即可

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

三、启动类main方法写错

就是把SpringApplication.run(XxxApplication.class,args);写成了SpringApplication.run(SpringApplication.class,args);

应该是本项目的启动类名.class,如:SpringApplication.run(启动类名.class,args);

四、移除排除项

我试了第四个解决方案移除排除项是可以完美解决我的问题。

五、我的解决方案

在网上搜集了几种可能出现的问题原因,但是我的都不是这些问题,找了很久,终于找到了,是与pom.xml有关的,具体如下:

错误写法:

<!-- 这里指定打war包的时不再需要tomcat相关的包,但是本地运行时必须注释掉,否则会报错 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>

正确写法:<scope>里的provided改为provide即可。

<!-- 这里指定打war包的时不再需要tomcat相关的包,但是本地运行时必须注释掉,否则会报错 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provide</scope>
        </dependency>

至于为什么还没有查清楚,后续再更新!

完结!

猜你喜欢

转载自blog.csdn.net/weixin_44299027/article/details/113590189
今日推荐