第一步
修改pom.xml
1.打包方式改成war
<packaging>jar</packaging>
替换成
<packaging>war</packaging>
如果没有“project”子节点没有“packaging”节点,直接复制添加即可
2.将spring-boot-starter-tomcat的scope属性设置为provided
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
当没有这个依赖时,直接复制添加即可
第二步
再spring-boot启动类同级目录,添加ServletInitializer类
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
public class ServletInitializer extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(Application.class);
}
}
上方的Application.class中的Appliation类为你的SpringBoot启动类
第三步
执行命令打包(或者使用GUI打包)
mvn clean package
执行到这一步,如果控制台没有出现ERROR就打包OK了,其war包在target目录下,将其复制到tomcat/webapps目录下启动服务器即可访问
http://host:port/tomcat-webapps下的项目名