1、说明:
由于SpringBoot默认访问无需项目名,所以打包成war的SpringBoot项目也需要部署成不需要输入项目名的方式,否则资源无法访问
2、pom.xml调整
2.1打包方式修改
<packaging>jar</packaging> 并更为:<packaging>war</packaging>
2.2添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
2.3排除嵌入式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>
2.4注释spring-boot-maven-plugin,使用maven-war-plugin
<!--
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin> -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
3. 启动类修改
继承SpringBootServletInitializer覆盖configure方法,示例:
public class Application extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(Application.class);
}
public static void main(String[] args) {
new SpringApplicationBuilder(Application.class).web(true).run(args);
}
}
4、外部tomcat一定要和springboot中tomcat一致,tomcat下载地址 https://archive.apache.org/dist/tomcat/tomcat-8/v8.5.31/bin/