Deploy a Spring Boot WAR into a Tomcat Server – 将Spring Boot WAR部署到Tomcat服务器上

最后修改: 2018年 5月 18日

中文/混合/英文(键盘快捷键:t)

1. Introduction

1.绪论

Spring Boot is a convention over configuration framework that allows us to create a production-ready setup of a Spring project, and Tomcat is one of the most popular Java Servlet Containers.

Spring Boot是一个重于配置的惯例框架,它允许我们创建一个Spring项目的生产就绪设置,而Tomcat是最受欢迎的Java Servlet容器之一。

By default, Spring Boot builds a standalone Java application that can run as a desktop application or be configured as a system service, but there are environments where we can’t install a new service or run the application manually.

默认情况下,Spring Boot构建了一个独立的Java应用程序,可以作为桌面应用程序运行,也可以配置为系统服务,但在有些环境中,我们无法安装新的服务或手动运行应用程序。

In contrast to standalone applications, Tomcat is installed as a service that can manage multiple applications within the same application process, avoiding the need for a specific setup for each application.

与独立的应用程序相比,Tomcat被安装为一个服务,可以在同一个应用程序中管理多个应用程序,避免了为每个应用程序进行特定的设置。

In this tutorial, we’ll create a simple Spring Boot application and adapt it to work within Tomcat.

在本教程中,我们将创建一个简单的Spring Boot应用程序,并使其在Tomcat中工作。

2. Setting up a Spring Boot Application

2.设置一个Spring Boot应用程序

Let’s set up a simple Spring Boot web application using one of the available starter templates:

让我们使用其中一个可用的启动模板建立一个简单的Spring Boot网络应用。

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId> 
    <version>2.4.0</version> 
    <relativePath/> 
</parent> 
<dependencies>
    <dependency> 
        <groupId>org.springframework.boot</groupId> 
        <artifactId>spring-boot-starter-web</artifactId> 
    </dependency> 
</dependencies>

There’s no need for additional configurations beyond the standard @SpringBootApplication, since Spring Boot takes care of the default setup.

除了标准的@SpringBootApplication之外,不需要额外的配置,因为Spring Boot已经处理了默认设置。

Then we’ll add a simple REST EndPoint to return some valid content for us:

然后我们将添加一个简单的REST EndPoint,为我们返回一些有效的内容。

@RestController
public class TomcatController {

    @GetMapping("/hello")
    public Collection<String> sayHello() {
        return IntStream.range(0, 10)
          .mapToObj(i -> "Hello number " + i)
          .collect(Collectors.toList());
    }
}

Finally, we’ll execute the application with mvn spring-boot:run, and start a browser at http://localhost:8080/hello to check the results.

最后,我们将用mvn spring-boot:run,执行该应用程序,并在http://localhost:8080/hello启动浏览器以检查结果。

3. Creating a Spring Boot WAR

3.创建一个Spring Boot WAR

Servlet containers expect the applications to meet some contracts to be deployed. For Tomcat the contract is the Servlet API 3.0.

Servlet容器期望应用程序满足一些合同才能被部署。对于Tomcat来说,该合同是Servlet API 3.0

To have our application meet this contract, we have to perform some small modifications in the source code.

为了使我们的应用程序符合这个合同,我们必须在源代码中进行一些小的修改。

First, we need to package a WAR application instead of a JAR. For this, we’ll change pom.xml with the following content:

首先,我们需要打包一个WAR应用程序而不是JAR。为此,我们将修改pom.xml,内容如下。

<packaging>war</packaging>

Next, we’ll modify the final WAR file name to avoid including version numbers:

接下来,我们将修改最终的WAR文件名,以避免包括版本号。

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

Then we’ll add the Tomcat dependency:

然后,我们将添加Tomcat的依赖关系。

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-tomcat</artifactId>
   <scope>provided</scope>
</dependency>

Finally, we’ll initialize the Servlet context required by Tomcat by implementing the SpringBootServletInitializer interface:

最后,我们将通过实现SpringBootServletInitializer接口来初始化Tomcat所需的Servlet上下文

@SpringBootApplication
public class SpringBootTomcatApplication extends SpringBootServletInitializer {
}

To build our Tomcat-deployable WAR application, we’ll execute the mvn clean package. After that, our WAR file is generated at target/spring-boot-deployment.war (assuming the Maven artifactId is “spring-boot-deployment”).

为了构建可部署Tomcat的WAR应用,我们将执行mvn clean软件包。之后,我们的WAR文件将在target/spring-boot-deployment.war生成(假设Maven的artifactId为 “spring-boot-deployment”)。

We should consider that this new setup makes our Spring Boot application a non-standalone application (if we want to have it working in standalone mode again, we can remove the provided scope from the tomcat dependency).

我们应该考虑到,这个新设置使我们的Spring Boot应用程序成为一个非独立的应用程序(如果我们想让它再次以独立模式工作,我们可以从tomcat依赖关系中删除provided范围)。

4. Deploying the WAR to Tomcat

4.将WAR部署到Tomcat

To have our WAR file deployed and running in Tomcat, we’ll need to complete the following steps:

为了让我们的WAR文件在Tomcat中部署和运行,我们需要完成以下步骤。

  1. Download Apache Tomcat and unpackage it into a tomcat folder
  2. Copy our WAR file from target/spring-boot-deployment.war to the tomcat/webapps/ folder
  3. From a terminal, navigate to the tomcat/bin folder and execute
    1. catalina.bat run (on Windows)
    2. catalina.sh run (on Unix-based systems)
  4. Go to http://localhost:8080/spring-boot-deployment/hello

This has been a quick Tomcat setup, so please check the guide on Tomcat Installation for a complete setup guide. There are also additional ways of deploying a WAR file to Tomcat.

这是一个快速的Tomcat设置,所以请查看Tomcat安装上的指南以获得完整的设置指南。还有其他一些将WAR文件部署到Tomcat的方法。

 5. Conclusion

5.总结

In this brief article, we created a simple Spring Boot application and turned it into a valid WAR application deployable on a Tomcat server.

在这篇简短的文章中,我们创建了一个简单的Spring Boot应用程序,并把它变成了一个可在Tomcat服务器上部署的有效WAR应用程序。

As always, the full source code of the examples is available over on GitHub.

一如既往,这些示例的完整源代码可在GitHub上获得over