1.更改Application启动类所在模块中的pom文件,将标签改为war2。去掉内置的Built-intomcatorg.springframework.bootspring-boot-starter-weborg.springframework.bootspring-boot-starter-tomcat3.添加servlet依赖javax.servletjavax.servlet-apiprovided4.添加war启动类classWarStarterApplicationextendsSpringBootServletInitializer{@OverrideprotectedSpringApplicationBuilderconfigure(SpringApplicationBuilderbuilder){//指向Application,返回springboot启动类nbuilder.sources(Application.class);}}完成以上步骤后,使用maven的package功能进行打包,放入tomcat中运行。这时候如果想在项目中继续使用启动类来运行项目,“UnabletostartServletWebServerApplicationContextduetomissingServletWebServerFactorybean”。报错,但在tomcat中可以正常运行。2021-08-3011:00:05错误SpringApplication:858-应用程序运行失败org.springframework.context.ApplicationContextException:无法启动Web服务器;嵌套异常是org.springframework.context.ApplicationContextException:由于缺少ServletWebServerFactorybean,无法启动ServletWebServerApplicationContext。在org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:157)在org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543)在org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:142)在org.springframework.boot.SpringApplication.refresh(SpringApplication.java:775)在org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397)在org.springframework.boot.SpringApplication.run(SpringApplication.java:316)在org.springframework.boot.SpringApplication.run(SpringApplication.java:1260)在org.springframework.boot.SpringApplication.run(SpringApplication.java:1248)在com.jimmy.Application.main(Application.java:25)原因:org.springframework.context.ApplicationContextException:由于缺少ServletWebServerFactorybean,无法启动ServletWebServerApplicationContext。在org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.getWebServerApplicationContext2atjava.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext).java:1m5ore出现这个错误的原因是上面第2步中去掉了内置的tomcat,放入tomcat的war包可以正常运行,因为tomcat已经自带了这个相关包裹。如果想在IDE中继续使用启动类启动项目,只需要去掉第2步中内置tomcat的步骤注释即可。