Spring Boot - Tomcat Deployment
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.
The code for Spring Boot Application class file for JAR file deployment is given below −
package com.tutorialspoint.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
SpringApplication.run(DemoApplication.class, args);
package com.tutorialspoint.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
@Override
return application.sources(DemoApplication.class);
SpringApplication.run(DemoApplication.class, args);
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 −
<start-class>com.tutorialspoint.demo.DemoApplication</start-class>
For Gradle, add the main class name in build.gradle as shown below −
mainClassName="com.tutorialspoint.demo.DemoApplication"
<packaging>war</packaging>
For Gradle, add the application plugin and war plugin in the build.gradle as shown below −
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.tutorialspoint.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
https://www.tutorialspoint.com/spring_boot/spring_boot_tomcat_deployment.htm 2/8
7/31/22, 7:46 PM Spring Boot - Tomcat Deployment
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@RestController
@Override
return application.sources(DemoApplication.class);
SpringApplication.run(DemoApplication.class, args);
@RequestMapping(value = "/")
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 −
https://www.tutorialspoint.com/spring_boot/spring_boot_tomcat_deployment.htm 3/8
7/31/22, 7:46 PM Spring Boot - Tomcat Deployment
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 −
https://www.tutorialspoint.com/spring_boot/spring_boot_tomcat_deployment.htm 4/8
7/31/22, 7:46 PM Spring Boot - Tomcat Deployment
https://www.tutorialspoint.com/spring_boot/spring_boot_tomcat_deployment.htm 5/8
7/31/22, 7:46 PM Spring Boot - Tomcat Deployment
pom.xml
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
<modelVersion>4.0.0</modelVersion>
https://www.tutorialspoint.com/spring_boot/spring_boot_tomcat_deployment.htm 6/8
7/31/22, 7:46 PM Spring Boot - Tomcat Deployment
<groupId>com.tutorialspoint</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>demo</name>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.8.RELEASE</version>
</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.tutorialspoint.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}")
https://www.tutorialspoint.com/spring_boot/spring_boot_tomcat_deployment.htm 7/8
7/31/22, 7:46 PM Spring Boot - Tomcat Deployment
group = 'com.tutorialspoint'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
mainClassName = "com.tutorialspoint.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.tutorialspoint.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
@Override
return application.sources(DemoApplication.class);
SpringApplication.run(DemoApplication.class, args);
@RequestMapping(value = "/")
https://www.tutorialspoint.com/spring_boot/spring_boot_tomcat_deployment.htm 8/8