前言:在去年年末写了一篇关于war包发布到tomcat的文章--springboot项目打war包部署到本地或centos7中的tomcat_lanren312的博客-CSDN博客,里面提到还有些问题没有解决,时隔3个多月,终于将疑惑全部解开。
step1: 直接上配置文件 一、修改打包方式,默认是jar包,加了才是war包二、排除内置的tomcatwar
三、添加tomcat依赖,或者添加servlet-api依赖,二者用其一,本人使用是添加tomcat依赖org.springframework.boot spring-boot-starter-weborg.springframework.boot spring-boot-starter-tomcat
四、主类继承SpringBootServletInitializer,重写configure方法..(ps:HeroesApplication是我的项目启动类,需要根据自己的项目修改)org.springframework.boot spring-boot-starter-tomcatprovided org.apache.tomcat tomcat-servlet-api8.5.56 provided
public class HeroesApplication extends SpringBootServletInitializer{
public static void main(String[] args) {
SpringApplication.run(HeroesApplication.class, args);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(HeroesApplication.class);
}
五、将打好的war包移动到你本地tomcat的webapps目录下
六、最重要的步骤,找到并打开 D:Program Filesapache-tomcat-8.5.56confserver.xml
ps: docbase的路径可以看上图,tomcat启动heroes.war会解析一个heroes的文件夹
1、本地开发登录地址 http:localhost:30000/heroes/login,部署Tomcat后yml文件中配置的port会失效,登录地址应该是 http:localhost:8080/heroes/login;
上篇博客提到登录地址一致是 http:localhost:8080/heroes/heroes/login,是因为没有在server.xml 中配置,登录也有问题,因为多了一个/heroes
2、在配置server.xml时如果不将appbase设置为空,访问地址是 http:localhost:8080/heroes/heroes/login,并且tomcat启动了两次
最后,如果对你有帮助就点个赞吧。



