前言
前面的文章的 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 中了。