第一步
首先,我们需要将pom.xml
中的打包方式改成war
1 |
<packaging>jar</packaging> |
替换成
1 |
<packaging>war</packaging> |
第二步
将spring-boot-starter-tomcat
的scope
属性设置为provided
1 2 3 4 5 |
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency> |
第三步
添加ServletInitializer
类
1 2 3 4 5 6 7 8 9 |
import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.web.support.SpringBootServletInitializer; public class ServletInitializer extends SpringBootServletInitializer { @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) { return builder.sources(Application.class); } } |
注:上方的Application.class
中的Appliation类为你的SpringBoot启动类。
第四步
在项目根目录下运行maven命令
1 |
mvn clean package |
执行到这一步,如果控制台没有出现ERROR就打包OK了,其war包在target目录下,将其复制到tomcat/webapps目录下启动服务器即可访问
后记
如果文章有任何纰漏或者问题,请在评论区留言哈,博主会第一时间进行改正,谢谢大家