一、设置pom.xml
1.设置/添加
<packaging>war</packaging>
2.移除嵌入式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>org.apache.tomcat</groupId> <artifactId>tomcat-servlet-api</artifactId> <version>8.0.36</version> <scope>provided</scope> </dependency>
4.修改启动类,增加SpringBootServletInitializer类继承
public class Application extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
5.重新初始化方法
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder)
{
return builder.sources(Application.class);
}
6.执行生成war包命令
执行命令:
mvn clean package
如果提示springboot错误: 找不到或无法加载主类,则需要在CMD中执行此命令
然后在项目中按F5刷新项目即可解决。
将生成的war包放到tomcat的webapps目录中,重新运行tomcat即可执行。