前言
前面的文章的 web 程序的樣例都是一個個獨立運行的 jar 包的形式。這種方式使用起來很方便,但是也有時候有需求要將 SpringBoot 開發的 web 程序放到一個 web 容器例如 Tomcat 中去運行。這個時候就需要將 SpringBoot 程序給打包成一個標準的 war 包文件才行。怎麼做呢,其實也很簡單,下面我們來演示一下。
創建標準 SpringBoot web 項目
根據前面的文章,我們在 IDEA 裏面創建一個標準的 SpringBoot 程序,他的 Maven 配置文件 pom.xml 內容如下
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.yanggaochao.demo</groupId>
<artifactId>war_demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>war_demo</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
然後我們編寫一個 Rest 服務接口如下
package com.yanggaochao.demo.war;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
/**
* Hello 服務
*
* @author : 楊高超
* @since : 2018-11-20
*/
@RestController
@RequestMapping("/api/v1/hello")
public class HelloService {
@RequestMapping(value = "/echo/{name}", method = RequestMethod.GET)
public String sayHello(@PathVariable String name) {
return "Hello," + name;
}
}
然後,我們配置 application.propertis 內容如下
server.servlet.context-path=/spring_boot_war
server.port=3333
運行主程序。那麼在瀏覽器中輸入地址 http://localhost:3333/spring_boot_war/api/v1/hello/echo/yanggch 就會在頁面上顯示 Hello,yanggch 的文本。
這就是一個標準的 SpringBoot Web 程序了。下面我們要把他改造成爲一個可以打包爲標準的 war 包文件,放到 Java Web 容器中運行的程序。
改造爲 War 包程序
首先,我們要修改我們的啓動類,讓他繼承一個 org.springframework.boot.web.servlet.support.SpringBootServletInitializer 類並且改寫 config 方法。修改後的啓動類變成
package com.yanggaochao.demo.war;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
/**
* War 樣例
*
* @author : 楊高超
* @since : 2018-11-20
*/
@SpringBootApplication
public class WarApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(WarApplication.class, args);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(WarApplication.class);
}
}
然後修改我們的 Maven 文件如下
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.yanggaochao.demo</groupId>
<artifactId>war</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>war</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>spring_boot_war</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
這裏有兩個改動,一個是 <packaging>war</packaging> 的值由 jar 改變爲了 war。第二個是在 <build> 標籤下增加了一個 <finalName>spring_boot_war</finalName> 。這是因爲當打包爲 war 以後, application.properties 文件就失效了,tomcat 中的 war 包的 context_path 默認和 war 包的文件名一致,而這個程序默認的 war 包名稱講師 war_demo-0.0.1-SNAPSHOT.jar 。這個顯然不太友好,所以我們增加 <finalName>spring_boot_war</finalName> 配置讓生成的 war 包和我們預想的 context_path 保持一致。然後,執行 mvn package 得到 target/spring_boot_war.war 文件。最後將這個文件放到 tomcat 的 webapps 目錄下啓動 tomcat。啓動完成後在瀏覽器中輸入 http://localhost:8080/spring_boot_war/api/v1/hello/echo/yanggch 。頁面上同樣會出現 Hello,yanggch 文本。這就表示我們的程序正確的以 War 包的形式發佈到 Tomcat 中了。