1、首先需要修改项目的打包方式,将package改为war
<packaging>war</packaging>
2、移除spring boot web中的嵌入式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> <version>3.1.0</version> <scope>provided</scope> </dependency>
4、新建一个类SpringBootStartApplication,继承SpringBootServletInitializer,并重写方法configure
import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.web.support.SpringBootServletInitializer; public class SpringBootStartApplication extends SpringBootServletInitializer { @Override protected SpringApplicationBuilder configure( SpringApplicationBuilder builder) { return builder.sources(Application.class); } }
5、进入pom.xml目录,执行mvn package。编译完成后target目录下会出现一个war文件。
6、将该war文件放入tomcat容器里,然后启动tomcat,即可。