- 在pom.xml中排除tomcat
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
- 导出tomcat的支持(关键)
<!--部署成war包时开启↓↓↓↓-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<!--部署成war包时开启↑↑↑↑-->
- 打包方式设置成war
<packaging>war</packaging>
- war包名称
<build>
<finalName>rengu</finalName>
</build>
- 启动类继承 SpringBootServletInitializer
@SpringBootApplication
public class DemoApplication extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(DemoApplication.class);
}
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
- 使用maven打包成war包,这里就可以获的一个rengu.war
- 自行下载tomcat (这里使用tomcat8.5) tomcat8.5下载
- 将war包拷贝到tomcat的webpps路径下
- bin文件夹下双击startup.bat即可启动
===================================================
附加tomcat相关配置
- 在conf/server.xml配置get请求编码格式
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" URIEncoding="UTF-8" />
- 设置log编码格式 打开tomcat/conf/logging.properties
找到java.util.logging.ConsoleHandler.encoding = UTF-8
修改为java.util.logging.ConsoleHandler.encoding = GBK
=================================
有坑点(找了1天,呜呜呜)
- 正常的demo打成war包是没有问题的,
- 使用完成的项目打成war包在tomcat中出现启动错误
- 项目中有过滤器(Filter)当有过滤器时候必须实现初始化销毁方法(init,destroy)
- 在java8中可以默认不进行实现