Spring Boot – Tomcat Deployment

By using Spring Boot application, we can create a war file to deploy into the web server. In this chapter, you are going to learn how to create a WAR file and deploy the Spring Boot application in Tomcat web server.

1. Spring Boot Servlet Initializer

The traditional way of deployment is making the Spring Boot Application @SpringBootApplication class extend the SpringBootServletInitializer class. Spring Boot Servlet Initializer class file allows you to configure the application when it is launched by using Servlet Container.

The code for Spring Boot Application class file for JAR file deployment is given below −

package com.maixuanviet.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication {
   public static void main(String[] args) {
      SpringApplication.run(DemoApplication.class, args);
   }
}

We need to extend the class SpringBootServletInitializer to support WAR file deployment. The code of Spring Boot Application class file is given below −

package com.maixuanviet.demo;

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

Setting Main Class

In Spring Boot, we need to mention the main class that should start in the build file. For this purpose, you can use the following pieces of code −

For Maven, add the start class in pom.xml properties as shown below −

<start-class>com.maixuanviet.demo.DemoApplication</start-class>

For Gradle, add the main class name in build.gradle as shown below −

mainClassName="com.maixuanviet.demo.DemoApplication"

Update packaging JAR into WAR

We have to update the packaging JAR into WAR using the following pieces of code −

For Maven, add the packaging as WAR in pom.xml as shown below −

<packaging>war</packaging>

For Gradle, add the application plugin and war plugin in the build.gradle as shown below −

apply plugin: ‘war’
apply plugin: ‘application’

Now, let us write a simple Rest Endpoint to return the string “Hello World from Tomcat”. To write a Rest Endpoint, we need to add the Spring Boot web starter dependency into our build file.

For Maven, add the Spring Boot starter dependency in pom.xml using the code as shown below −

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

For Gradle, add the Spring Boot starter dependency in build.gradle using the code as shown below −

dependencies {
compile(‘org.springframework.boot:spring-boot-starter-web’)
}

Now, write a simple Rest Endpoint in Spring Boot Application class file using the code as shown below −

package com.maixuanviet.demo;

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.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
public class DemoApplication  extends SpringBootServletInitializer {
   @Override
   protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
      return application.sources(DemoApplication.class);
   }
   public static void main(String[] args) {
      SpringApplication.run(DemoApplication.class, args);
   }
   
   @RequestMapping(value = "/")
   public String hello() {
      return "Hello World from Tomcat";
   }
}

Packaging your Application

Now, create a WAR file to deploy into the Tomcat server by using Maven and Gradle commands for packaging your application as given below −

For Maven, use the command mvn package for packaging your application. Then, the WAR file will be created and you can find it in the target directory as shown in the screenshots given below −

Maven MVN Package
Maven Packaging Application Target Directory

For Gradle, use the command gradle clean build for packaging your application. Then, your WAR file will be created and you can find it under build/libs directory. Observe the screenshots given here for a better understanding −

Gradle Clean Build Command
Maven Packaging Application Target Directory

Deploy into Tomcat

Now, run the Tomcat Server, and deploy the WAR file under the webapps directory. Observe the screenshots shown here for a better understanding −

Tomcat Web Application Maneger
webApps Directory

After successful deployment, hit the URL in your web browser http://localhost:8080/demo-0.0.1-SNAPSHOT/ and observe that the output will look as shown in the screenshot given below −

Successful Deployment Screenshot

The full code for this purpose is given below.

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.maixuanviet</groupId>
   <artifactId>demo</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <packaging>war</packaging>
   <name>demo</name>
   <description>Demo project for Spring Boot</description>
   
   <parent>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-parent</artifactId>
      <version>1.5.8.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>
      <start-class>com.maixuanviet.demo.DemoApplication</start-class>
   </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>

build.gradle

buildscript {
   ext {
      springBootVersion = '1.5.8.RELEASE'
   }
   repositories {
      mavenCentral()
   }
dependencies {
      classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
   }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'war'
apply plugin: 'application'

group = 'com.maixuanviet'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
mainClassName = "com.maixuanviet.demo.DemoApplication"

repositories {
   mavenCentral()
}
dependencies {
   compile('org.springframework.boot:spring-boot-starter-web')
   testCompile('org.springframework.boot:spring-boot-starter-test')
}

The code for main Spring Boot application class file is given below −

package com.maixuanviet.demo;

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.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
public class DemoApplication  extends SpringBootServletInitializer {
   @Override
   protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
      return application.sources(DemoApplication.class);
   }
   public static void main(String[] args) {
      SpringApplication.run(DemoApplication.class, args);
   }
   
   @RequestMapping(value = "/")
   public String hello() {
      return "Hello World from Tomcat";
   }
}

Related posts:

Java Program to Generate a Random UnDirected Graph for a Given Number of Edges
Extra Login Fields with Spring Security
Hướng dẫn sử dụng biểu thức chính quy (Regular Expression) trong Java
Java Program to Perform Searching in a 2-Dimension K-D Tree
Introduction to Spring Boot CLI
Hướng dẫn Java Design Pattern – DAO
Introduction to Apache Commons Text
Jackson Annotation Examples
Giới thiệu Google Guice – Dependency injection (DI) framework
Sử dụng JDBC API thực thi câu lệnh truy vấn dữ liệu
Hướng dẫn kết nối cơ sở dữ liệu với Java JDBC
Giới thiệu JDBC Connection Pool
Java Program to Implement D-ary-Heap
Java Program to Check if a Point d lies Inside or Outside a Circle Defined by Points a, b, c in a Pl...
Jackson JSON Views
Java Program to Implement Radix Sort
Adding Shutdown Hooks for JVM Applications
Java Program to Describe the Representation of Graph using Incidence List
Java Program to Implement Branch and Bound Method to Perform a Combinatorial Search
XML Serialization and Deserialization with Jackson
Returning Image/Media Data with Spring MVC
ETL with Spring Cloud Data Flow
Configure a RestTemplate with RestTemplateBuilder
Converting Between an Array and a Set in Java
Introduction to Eclipse Collections
Java IO vs NIO
Hướng dẫn Java Design Pattern – Proxy
How to Manually Authenticate User with Spring Security
Introduction to Java 8 Streams
Java Program to Solve Travelling Salesman Problem for Unweighted Graph
Java Map With Case-Insensitive Keys
The “final” Keyword in Java