一、修改打包类型
在pom.xml中修改
<packaging>war</packaging>
二、移除嵌入式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>
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency>
三、添加servlet-api的依赖
<dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> </dependency>
四、修改启动方式,继承SpringBootServletInitializer 类,并重写初始化方法
实现方式1:直接修改启动类
/** * 修改启动类,继承 SpringBootServletInitializer 并重写 configure 方法 */ @SpringBootApplication public class Application extends SpringBootServletInitializer { public static void main(String[] args) { SpringApplication.run(Application.class, args); } @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) { return builder.sources(Application.class); } }
实现方式2:通过自定义类
/** * 启动类保持不变 */ @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
/** * 自定义类,继承 SpringBootServletInitializer 并重写 configure 方法 */ public class SpringBootStartApplication extends SpringBootServletInitializer { @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) { // 注意这里要指向原先用main方法执行的Application启动类 return builder.sources(Application.class); } }