Spring Boot应用程序除了生成JAR文件直接运行外,也可以创建成一个WAR文件部署到Web服务器中。在本章节中,我将为你展示如何使用Spring Boot创建WAR文件并在Tomcat Web容器中进行部署。
1. Spring Boot Servlet初始化程序
如果想要在Tomcat Web服务器中部署Spring Boot应用程序,需要让被@SpringBootApplication注解注释的的主类继承SpringBootServletInitializer类,并覆盖configure方法。
下面的代码片段是使用JAR文件运行Spring Boot应用程序的主文件代码:
package com.ramostear.spring.boot.deploytomcat;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @author ramostear
*/
@SpringBootApplication
public class DeployTomcatApplication {
public static void main(String[] args) {
SpringApplication.run(DeployTomcatApplication.class, args);
}
}
现在,我们需要扩展SpringBootServletInlitializer类以支持WAR文件部署。扩展后的主类代码如下:
package com.ramostear.spring.boot.tutorial2;
@SpringBootApplication
public class Application extends SpringBootServletInitializer{
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder){
return builder.sources(Application.class,args);
}
public static void main(String[]args){
SpringBootApplication.run(Application.class,args);
}
}
2. 设置主类
对于Spring Boot应用程序,你需要明确的指出在maven构建项目是主类的位置。你需要在pom.xml文件加入如下的代码:
<start-class>com.ramostear.spring.boot.tutorial2.Application</start-class>
3. 更新打包方式
想要将Spring Boot应用程序从jar包更换为war包,还需要修改pom.xml中的packaging配置:
<packaging>war</packaging>
现在,我们可以编写一个简单的REST Endpoint来返回字符串“Hey Spring Boot,I am from tomcat server.”。为了演示此功能,我们还需要将Spring Boot Web Starter添加到pom.xml文件中:
<dependencies>
...
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
...
</dependencies>
接下来,我们将在主类中编写一个简单的REST Endpoint:
package com.ramostear.spring.boot.deploytomcat;
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;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author ramostear
*/
@SpringBootApplication
@RestController
public class DeployTomcatApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(DeployTomcatApplication.class, args);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(DeployTomcatApplication.class);
}
@GetMapping("/")
public String sayHello(){
return "Hey Spring Boot,I am from tomcat server.";
}
}
4. 打包Spring Boot应用程序
我们将使用Maven打包命令创建一个可以在Tomcat服务器中运行的WAR文件。使用Maven命令:mvn:package打包应用程序。你可以在当前工程下的target目录中找到war文件:
C:\~projectdir> mvn package
控制台输出:
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building deploy-tomcat 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:3.1.0:resources (default-resources) @ deploy-tomcat ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] Copying 0 resource
.....
[INFO]
[INFO] --- spring-boot-maven-plugin:2.1.3.RELEASE:repackage (repackage) @ deploy-tomcat ---
[INFO] Replacing main artifact with repackaged archive
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 10.273 s
[INFO] Finished at: 2019-03-04T23:34:12+08:00
[INFO] Final Memory: 38M/288M
[INFO] ------------------------------------------------------------------------
Process finished with exit code 0
5 . 部署到Tomcat
现在,运行Tomcat服务器,并在webapps目录下部署准备好的war文件。你也可以登录Tomcat的管理界面上传war文件并部署:
成功部署war文件后,代开浏览器,并在浏览器地址栏输入:http://localhost:8080/deploy-tomcat-0.0.1-SNAPSHOT/ ,观察页面输出结果:
6 . 主要代码
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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.ramostear</groupId>
<artifactId>deploy-tomcat</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>deploy-tomcat</name>
<description>Demo project for Spring Boot</description>
<properties>
<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>
<packaging>war</packaging>
</project>
DeployTomcatApplication.java文件:
package com.ramostear.spring.boot.deploytomcat;
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;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author ramostear
*/
@SpringBootApplication
@RestController
public class DeployTomcatApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(DeployTomcatApplication.class, args);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(DeployTomcatApplication.class);
}
@GetMapping("/")
public String sayHello(){
return "Hey Spring Boot,I am from tomcat server.";
}
}