①将maven打包方式改为war
<packaging>war</packaging>
②在pom文件中引入依赖
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
③在启动类的同级目录下新建一个类
public class ServletInitializer extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(启动类.class);
}
}
然后就可以maven install了,将war包放到tomcat下,启动就可以访问了。端口就是tomcat的端口,访问地址默认要带有你的war包名称
参考文章:https://blog.csdn.net/qq_33512843/article/details/80951741