Spring Boot - jar & war

2020/11/25

Deployed by jar

這裡有完整的討論與官方鏈接

Deployed by war

因為Spring Boot預設有嵌入Tomcat,並且使用jar來部署環境,所以如果要使用war部署到web container,需要依照下列步驟:

步驟1:添加starter-web依賴

<dependencies>
    <dependency> 
        <groupId>org.springframework.boot</groupId> 
        <artifactId>spring-boot-starter-web</artifactId> 
    </dependency> 
</dependencies>

步驟2:指定maven打包war:

<packaging>war</packaging>

步驟3:指定war文件名稱:

<build>
    <finalName>${artifactId}</finalName>
    ... 
</build>

或是使用新版的語法:

<build>
    <finalName>${project.artifactId}</finalName>
    ... 
</build>

原因:

The expression ${artifactId} is deprecated. Please use ${project.artifactId} instead.

步驟4:啟動類繼承SpringBootServletInitializer並overrideconfigure()

@SpringBootApplication
public class Application extends SpringBootServletInitializer {
  public static void main(String[] args) {
      SpringApplication.run(Application.class, args);
  }

  @Override
  protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
      return builder.sources(Application.class);
  }
}

如果沒有步驟4的繼承(extends)SpringBootServletInitializer與改寫(override)configure,就會出現錯誤:

Spring Boot application gives 404 when deployed to Tomcat

關於此錯誤的討論:

官方文件:

部署教程:

SpringBootServletInitializer說明:



一位有故事的程序員

(Best Regard)

Show Disqus Comments

Post Directory

扫码关注公众号:吹雪
发送 290992
即可立即永久解锁本站全部文章