IDEA创建SpringleBoot Maven项目时服务器启动报错,错误项如下:
2023-03-10 11:24:10.361 INFO 5948 --- [ main] demo2020jsj2.Demo2020jsj2Application : Starting Demo2020jsj2Application on 张坚 with PID 5948 (D:\untitled2\target\classes started by Administrator in D:\untitled2)
2023-03-10 11:24:10.363 INFO 5948 --- [ main] demo2020jsj2.Demo2020jsj2Application : No active profile set, falling back to default profiles: default
2023-03-10 11:24:10.435 WARN 5948 --- [ main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: 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.
2023-03-10 11:24:10.450 ERROR 5948 --- [ main] o.s.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:161) ~[spring-boot-2.3.7.RELEASE.jar:2.3.7.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:545) ~[spring-context-5.2.12.RELEASE.jar:5.2.12.RELEASE]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:143) ~[spring-boot-2.3.7.RELEASE.jar:2.3.7.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:758) [spring-boot-2.3.7.RELEASE.jar:2.3.7.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:750) [spring-boot-2.3.7.RELEASE.jar:2.3.7.RELEASE]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:405) [spring-boot-2.3.7.RELEASE.jar:2.3.7.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) [spring-boot-2.3.7.RELEASE.jar:2.3.7.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1237) [spring-boot-2.3.7.RELEASE.jar:2.3.7.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226) [spring-boot-2.3.7.RELEASE.jar:2.3.7.RELEASE]
at demo2020jsj2.Demo2020jsj2Application.main(Demo2020jsj2Application.java:9) [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.7.RELEASE.jar:2.3.7.RELEASE]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.createWebServer(ServletWebServerApplicationContext.java:177) ~[spring-boot-2.3.7.RELEASE.jar:2.3.7.RELEASE]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:158) ~[spring-boot-2.3.7.RELEASE.jar:2.3.7.RELEASE]
... 9 common frames omitted
Process finished with exit code 1

把你的pom依赖贴一下吧。估计是依赖的问题,
使用外部Tomcat还是springboot内置的?使用外部的要配置下,并且要把内置的屏蔽
检查一下是不是添加了@SpringBootApplication注解。springboot的web项目必须要加。
@SpringBootApplication
public class YourAppliationName extends SpringBootServletInitializer{
public static void main(String[] args) {
SpringApplication.run(YourAppliationName.class, args);
}
}
如果不是web项目,需要在application.properties中可以进行添加:spring.main.web-application-type=none
这个错误是由于缺少ServletWebServerFactory bean导致的,可以在pom.xml文件中添加如下依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
这个依赖会自动引入Tomcat作为Servlet容器,并且会自动配置ServletWebServerFactory bean。如果需要使用其他的Servlet容器,可以在pom.xml文件中添加相应的依赖,并进行相应的配置。最后,需要重新构建和部署项目,确保ServletWebServerFactory bean能够正确加载和使用。
参考于:Cursor 应用