1. 程式人生 > >springboot打包war,tomcat執行及常見問題

springboot打包war,tomcat執行及常見問題

一、修改打包形式為war

在pom.xml裡設定
<packaging>war</packaging>

二、過濾springboot預設嵌入式tomcat外掛

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
<!-- 過濾springboot預設嵌入式tomcat外掛 -->
    <exclusions>
<exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion> </exclusions> </dependency>

三、加入servlet-api的依賴

<dependency>
    <groupId>javax.servlet</groupId
>
<artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>provided</scope> </dependency>

四、更改生成war包名

pom.xml - build 加入finalName。避免生成war包名歧義

    <build>
        <finalName>spring-boot</finalName>
        <plugins
>
<plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>

四、繼承springboot啟動類並重寫configure方法

1)建立java類SpringBootStartApplication
2)繼承SpringBootServletInitializer 並重寫configure

public class SpringBootStartApplication extends SpringBootServletInitializer {
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
    // 此處Application.class替換為springboot預設啟動類
    return builder.sources(Application.class);
    }
}

五、打包

mvn clean package

注意:
系統環境系統變數jdk版本與打包專案的jdk版本一致,否則會出現tomcat啟動成功,首頁正常訪問而各個路徑均404