English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

SpringBoot Tomcat Deployment

In this section, we will learn how to deploy a Spring Boot application on the Tomcat Server.

It includes three steps:

Set up the Spring Boot application Create Spring Boot WAR Deploying WAR to Tomcat

Example

Let's create a Maven example that can be deployed on Tomcat.

Set up the Spring Boot application

Steps1: Open Spring Initializr http: //start.spring.io .

Steps2: Provide Group Name. We provide com.w3codebox.

Steps3: Provide Artifact ID. We provide spring-boot-war-deployment-example.

Steps4: Add Spring Web Dependencies

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

Steps5: Click Generate (Generate) button. It wraps all the specifications related to the project and downloads them in our local system. jar file.

Steps6: Extract jar files.

Steps7: ImportPlease follow the following steps:

Files->Import->Existing Maven project->Next->Browse->Select project folder->Finish

After importing the project, we can access the IDE's Package Explorer The following directory structure can be seen in part.

Steps8: In the package com.w3codebox In the directory created by the Controller class. We created a named The class DemoRestController.

Within the controller class, we define a method that returns a string. hello().

DemoRestController.java

package com.w3codebox;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class DemoRestController 
{
@GetMapping("/hello"
public String hello() 
{
return "Hello User, have a nice day.";
}
}

Steps9: Run as a Java application SpringBootWarDeploymentExampleApplication.java file.

Steps10: Open a browser and call the URL http: //localhost: 8080/hello.

Note: Make sure the application is running normally before proceeding.

Create Spring Boot WAR

It utilizes the Servlet of the Spring Framework to create a Spring Boot WAR 3.0 support and allows us to configure the application when the Servlet container starts. To create a WAR for deployment, there is threesteps:

extend SpringBootServletInitializer class. Mark the embedded servlet container asprovided. Wrap JAR Update to

Let's implement the above three steps in the application.

Steps11: open SpringBootWarDeploymentExampleApplication.java file and initialize the Servlet context required by Tomcat. To achieve the same purpose, it extends SpringBootServletInitializer interface.

public class SpringBootWarDeploymentExampleApplication extends SpringBootServletInitializer
{
}

Steps12: override Configure method.

@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) 
{
return application.sources(SpringBootWarDeploymentExampleApplication.class);
}

SpringBootWarDeploymentExampleApplication.java

package com.w3codebox;
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;
@SpringBootApplication
public class SpringBootWarDeploymentExampleApplication extends SpringBootServletInitializer
{
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) 
{
return application.sources(SpringBootWarDeploymentExampleApplication.class);
}
public static void main(String[] args) 
{
SpringApplication.run(SpringBootWarDeploymentExampleApplication.class, args);
}
}

Steps13: open pom.xml file, and mark the Servlet container (Tomcat) as provided.

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

Steps14: We need to deploy WAR file, so the package type is changed to WAR in the pom.xml file.

<packaging>war</packaging>

Steps15: using  Mark the modification of the final WAR file name to avoid containing the version number. We created a file named web-services The WAR file.

<finalName>web-services</finalName>

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.w3codebox</groupId>
<artifactId>spring-boot-war-deployment-example</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>spring-boot-war-deployment-example</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.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-tomcat</artifactId>
<scope>provided</scope>
</dependency>  
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>web-services</finalName>
 <plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>     
</build>
</project>

To build a deployable Tomcat WAR application, we execute maven clean package. After that, in /target/abc.war (where abc Assumed as Artifact Id) to generate our WAR file. We should consider that this new setting makes our Spring Boot Application Non-independentApplication.

Steps16: Create WAR File":

Right-click the project-> Run As-> 5 Maven Build

A   Edit Configuration dialog box appears on the screen.

The17Step: In  Targettab and write  New Installation, then check  Skip Tests. Click  Applyand  Runbutton.

After the WAR file is successfully created, it will display   WAR file pathand messages   BUILD SUCCESS As shown in the console below.

Steps18: Copy  pathand access the application's   target Folder. We found the WAR file with the same name as specified in the pom.xml file in the target folder. In our case, the path is:

C:\Users\Anubhav\Documents\workspace-sts-3.9.9.RELEASE\spring-boot-war-deployment-example\target
 

Deployment adds the WAR file to Tomcat

To deploy the WAR file, follow the steps below:

Steps19: Download and Install   Apache Tomcat Server (if not installed).

Steps20: Copy the WAR file(web-services.war)and paste it intowebappsfolder. In our example, the location of the webapps folder is:

C:\Program Files\Apache Software Foundation\Tomcat 8.5\webapps


Steps21: Now open the command prompt and type the following command:

C:\Cd Program Files\Apache Software Foundation\Tomcat  8.5\bin
C:\Cd Program Files\Apache Software Foundation\Tomcat  8.5\bin>startup


StartCommand to start the Tomcat server and deploy the WAR file as follows.

The following figure shows that the WAR has been successfully deployed.

Steps23: Open a browser and call the URL http://localhost:8080/web-services/hello. It returns the message Hello, user!.