GeXiangDong

精通Java、SQL、Spring的拼写,擅长Linux、Windows的开关机

0%

把spring boot项目编译成war

修改启动类

改成继承org.springframework.boot.web.servlet.support.SpringBootServletInitializer, 例如:

1
2
3
4
5
6
7
8
9
10
11
12
13
package cn.devmgr.tutorial;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;

@SpringBootApplication
public class Application extends SpringBootServletInitializer{

public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}

修改pom文件

改packaging方式为war

1
<packaging>war</packaging>

改依赖

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
<!-- 排除掉内嵌的tomcat,为了减少war体积,也为了避免和tomcat服务器冲突 -->
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>

<!-- 去掉了内嵌的tomcat后,需要增加servlet-api,否则编译会出错了 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<scope>provided</scope>
</dependency>

注意事项

spring boot 2.0项目需要在tomcat 8.5以上版本环境下运行