Spring Boot - jar & war
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
關於此錯誤的討論:
官方文件:
部署教程:
- Deploy a Spring Boot WAR into a Tomcat Server | Baeldung
- How to make a Spring Boot Jar into a War to deploy on tomcat | Software Developer Blog
SpringBootServletInitializer
說明:
- Spring Boot - SpringBootServletInitializer Examples
- SpringBootServletInitializer Class in Spring Boot
- SpringBootServletInitializer tutorial - deploying Spring Boot application from WAR