Moment For Technology

Spring Boot 2 Deployment: Deploy as a War package

Posted on Nov. 27, 2023, 9:08 p.m. by Jonathan Richards
Category: The back-end Tag: The back-end Spring Boot

1. Introduction

Spring Boot provides built-in Tomcat, Undertow, and Jetty Servlet Web containers. Out of the box, let's quickly start a Web application with a JAR. However, in some scenarios we may also need to deploy our Spring Boot container conventionally as a War. At this moment we need using SpringBootServletInitializer to implement.

2. SpringBootServletInitializer

SpringBootServletInitializer WebApplicationInitializer implementation, it is from the deployment in conventional WAR file on the Web container to run Spring applications Boot. The class will be the Servlet Filter and ServletContextInitializer Bean from the application context is bound to the server. SpringBootServletInitializer class also allows us to override the SpringApplicationBuilder configure (SpringApplicationBuilder application) methods to configure by Applications run by the Servlet container.

3. Spring Boot War deployment

Let's go through the detailed Spring Boot War deployment steps.

3.1 Changing the packaging mode to War

Modify the pom. XML file of the Spring Boot project to change the packaging method to WAR.

The default jar package jar is war package war

3.2 Exclude embedded Web containers.

The default is to use the embedded Tomcat Web container. If you have previously used built-in Jetty and Undertow, be sure to remove the relevant Starter dependency. Then we can handle it in two ways:

  • Methods a

Tomcat embedded in Spring Boot is already integrated with the spring-boot-starter-Web package by default, so we'll exclude it.

dependency groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-web/artifactId exclusions exclusion groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-tomcat/artifactId /exclusion  /exclusions /dependencyCopy the code

This way we not rule out the Servlet Api dependence, SpringBootServletInitializer need to rely on the Servlet Api, So we'll add it (make sure the versionNumber version is compatible with your external Tomcat version).

    /dependencyCopy the code

  • Method 2

We override the default built-in Tomcat by introducing spring-boot-starter-Tomcat and setting the scope to provided.

     /dependency                      Copy the code

3.3 implementation SpringBootServletInitializer interface

The realization of the new SpringBootServletInitializer class ServletInitializer is as follows:

package cn.felord.war; import org.springframework.boot.builder.SpringApplicationBuilder; import; /** * @author Felordcn */ public class ServletInitializer extends SpringBootServletInitializer { @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(WarSpringBootApplication.class); }}Copy the code

Where WarSpringBootApplication is the SpringBoot entry class, it is the same as before:

package cn.felord.war; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * @author Felordcn */ @SpringBootApplication public class WarSpringBootApplication { public static void main(String[] args) {, args); }}Copy the code

3.4 Compiling and Packaging

Compile with maven command MVN clean package, wait a moment, get ${artifactId}-${version}. War, and you can run the Spring Boot application in traditional WAR deployment mode.

4. To summarize

In general, the way War deploys Spring Boot is not complicated. If you still have doubts, please refer to the DEMO, which can be obtained by following the public account: Felordcn reply war.

Follow our public id: Felordcn for more information

Personal blog:

About (Moment For Technology) is a global community with thousands techies from across the global hang out!Passionate technologists, be it gadget freaks, tech enthusiasts, coders, technopreneurs, or CIOs, you would find them all here.