springboot 项目如果要打成war包,部署到tomcat,并不是把pom中jar改成war就可以,还需要其他几个额外步骤
1 把pom中打包类型改为war
<packaging>war</packaging>
2 在springboot starterweb依赖中剔除 tomcat
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <!-- 去除内嵌tomcat --> <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> <version>3.1.0</version> <scope>provided</scope> </dependency>
4 启动类继承springBootServletInitializer 并重写方法。war包配置完成
public class CsrfdemoApplication extends SpringBootServletInitializer { public static void main(String[] args) { SpringApplication.run(CsrfdemoApplication.class, args); } @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) { return builder.sources(this.getClass()); } }