Springboot项目打war包流程及过程中出现的问题
Posted JimmyLu
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Springboot项目打war包流程及过程中出现的问题相关的知识,希望对你有一定的参考价值。
1.将Application启动类所在的module中的pom文件,把<packaging>标签修改成
<packaging>war</packaging>
2.移除自带内置tomcat
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
3.添加servlet依赖
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<scope>provided</scope>
</dependency>
4.增加war的启动类
class WarStarterApplication extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
// 指向Application这个springboot启动类
return builder.sources(Application.class);
}
}
完成以上步骤后,使用maven的package功能打包即能放到tomcat中运行。
此时,如果想在项目中继续使用启动类运行项目,会出现“ Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean.”报错,但在tomcat中是能正常运行的。
2021-08-30 11:00:05 ERROR SpringApplication:858 - 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:157)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543)
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:142)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:775)
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:316)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1260)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1248)
at com.jimmy.Application.main(Application.java:25)
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:206)
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.createWebServer(ServletWebServerApplicationContext.java:180)
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:154)
... 8 more
出现该错误的原因是上述步骤2,移除了内置的tomcat,而放在tomcat中的war包能正常运行,是因为tomcat中已自带了这个相关的包。
如果想继续在IDE中使用启动类启动项目,只需要将步骤2移除内置tomcat的步骤注释即可。
以上是关于Springboot项目打war包流程及过程中出现的问题的主要内容,如果未能解决你的问题,请参考以下文章
使用idea对springboot项目打war包及遇到的问题总结
使用idea对springboot项目打war包及遇到的问题总结