Web application could not be started as there was no org.springframework.boot.web.servlet.server.Ser

1、问题概述?

启动springboot工程的时候,报错了,我总结了三点原因

Web application could not be started as there was no org.springframework.boot.web.servlet.server.ServletWebServerFactory bean defined in the context.

Action:

Check your applications dependencies for a supported servlet web server.

Check the configured web application type.

process finished with exit code 1

原因1:启动类问题

原因2:缺少配置

原因3:配置冲突

2、如何解决这个问题?

2.1、启动类注意点

1、需要使用注解@SpringBootApplication

2、SpringApplication.run括号中使用当前类的类名。

@SpringBootApplication
public class HazqwebApplication {

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

}

2.2、pom.xml中配置类必须配置如下

springboot-boot-starter-web这个是启动的必须要配置,不能缺少。

下面提供一个开发常用的pom.xml配置包

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
   
### 解决方案 在Spring Boot应用程序中,`ServletWebServerFactory` 是用于配置嵌入式Web服务器(如Tomcat、Jetty或Undertow)的核心组件之一。如果缺少该Bean,则可能是因为某些配置错误或者依赖冲突引起的。 #### 原因分析 1. **缺失必要的starter依赖** 如果未引入 `spring-boot-starter-web` 或其他提供 Web 支持的 Starter 依赖,可能导致无法自动创建 `ServletWebServerFactory` Bean[^1]。 2. **不兼容的依赖版本** 不同版本的 Spring Boot 和其子模块可能存在不兼容的情况,这可能会破坏默认的自动化配置机制[^2]。 3. **手动禁用了嵌入式容器** 在 `application.properties` 或 `application.yml` 文件中设置了如下属性: ```properties spring.main.web-application-type=none ``` 这会显式关闭嵌入式Web服务器的支持[^3]。 --- #### 解决方法 ##### 方法一:确认是否引入了正确的Starter依赖 确保项目的 `pom.xml` 中包含了以下依赖项: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> ``` 如果没有上述依赖,请将其添加至项目中并重新构建项目。 ##### 方法二:检查是否存在冲突的依赖 运行以下命令来检测潜在的依赖冲突: ```bash mvn dependency:tree ``` 如果有多个不同版本的 Spring Boot 子模块被引入,建议调整 `<dependencyManagement>` 部分以锁定一致的版本号。 ##### 方法三:移除不必要的配置 如果确实不需要嵌入式Web服务器支持,可以保留当前设置 (`spring.main.web-application-type=none`) 并自行管理外部部署环境。但如果需要启用内置服务器,请删除相关禁用配置或将属性更改为: ```properties spring.main.web-application-type=servlet ``` ##### 方法四:自定义配置 `ServletWebServerFactory` 当默认行为不足以满足需求时,可以通过声明一个自定义的 `ServletWebServerFactory` 来解决问题。例如,在某个 Configuration 类中加入以下代码片段: ```java import org.springframework.boot.web.server.WebServerFactoryCustomizer; import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory; import org.springframework.context.annotation.Bean; public class CustomWebConfig { @Bean public WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> webServerFactoryCustomizer() { return factory -> { factory.setPort(8090); // 设置端口号或其他参数 }; } } ``` 这样能够覆盖默认的行为,并允许进一步定制化。 --- ### 总结 以上四种方式涵盖了大多数情况下遇到 “missing ServletWebServerFactory bean” 错误的原因及其解决方案。具体采用哪种取决于实际应用场景以及现有架构设计。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

雾林小妖

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

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

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

打赏作者

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

抵扣说明:

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

余额充值