在实际工作中,我们可能需要将一个SpringBoot的项目打成WAR包,采用传统的方式进行部署,下面讲述将项目打成WAR包的步骤并将其部署在TOMCAT下。
<groupId>cn.hadoopx</groupId>
<artifactId>servicex-system</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>servicex-system</name>
<packaging>war</packaging>
<description>系统基础管理服务</description>
如果未添加下面的依赖,打成的WAR包内会包含关于内置TOMCAT的多余的JAR包,如果实际使用的TOMCAT与内置的TOMCAT版本一致的话,并不影响项目的部署与运行,如果不一致则会出现项目版本冲突的问题。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
需要按如下方式修改,第②步是移除多余的打包雨来,第③步是移除内置的WEB容器。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!-- 移除内置的WEB容器 -->
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
新增一个重写 configure 方法的,实现 SpringBootServletInitializer 的子类。
public class MySpringBootServletInitializer extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
// 程序的入口
return application.sources(ServicexApplication.class);
}
}
使用常规的方式打包,一般将WAR包放在:apache-tomcat-9.0.39/webapps/目录下。

当采用WAR部署到TOMCAT的WEB容器中时,在application-dev.yml中通过server.port: 9099设置的端口不在起作用,如果要修改端口,则需要修改TOMCAT的WEB容器的端口为9099。
在前后端分离的项目中,当使用JAR包部署且未设置访问前缀的情况下,前端的访问路径一般为:http://localhost:9099/+XXX,当使用WAR包部署时,访问路径变为:http://localhost:9099/servicex-test-1.0-SNAPSHOT/+XXX。
SWAGGER:http://localhost:9099/servicex-test-1.0-SNAPSHOT/swagger-ui.html