Java, Software Development, Spring, Spring Boot

Convert Spring Boot to Deployable WAR for Tomcat

This post demonstrates how to convert your Spring Boot web application to a deployable WAR file for Tomcat.

Create a Spring Boot Web Application

Step 1: Create a new project using Spring Initialzr

Click Next.

Step 2: Provide Project Details

Notice the Packaging is set Jar.

Choose Web and Thymeleaf. Click Next.

Step 3: Complete New Project

Click Finish.

Once the project creation is complete, you’ll have a project with this structure:

Create a Simple Controller

Create a simple controller the displays a text on a web page.

Sample Output

Our simple controller displays the following.

Convert to Deployable WAR for Tomcat

Step 1: Modify SpringBootToTomcatApplication

Modify SpringBootToTomcatApplication to:

  1. Extend SpringBootServletInitializer
  2. Override the configure method

Step 2: Change POM packaging from JAR to WAR

Open your pom.xml up and change packaging from jar to war.

Packaging the WAR

When you run the mvn package this time, WAR files are now created.

war.original

There are two (2) files generated – .war and .war.original

The .war.original file doesn’t contain Spring Framework classes to boot your application up; while, .war includes all the needed Spring Framework classes.

So, which one to use in Tomcat?

Use the .war file.

Download the codes

To aid you better in understanding the changes I did, I uploaded the “before” and “after” projects so that you can see the differences made.

  1. https://github.com/Turreta/springboot-to-tomcat-war-start
  2. https://github.com/Turreta/springboot-to-tomcat-war-end

Testing WAR in Tomcat

In IntelliJ

Step 1: Go to File > Settings

Proceed to File > Settings. Click the green cross (+).

Choose Tomcat Server.

Provide the home path to your Tomcat.

Then, click Ok.

Step 2: Set Run/Debug Configurations

Choose Tomcat Server > Local

Go to the Deployment tab. Click the green cross (+) and choose External Source…

Proceed to the path where the .war file was generated.

Then, choose the .war file.

Then, click Ok.

Step 3: Start web app from IntelliJ

Now, your web application is ready to be deployed in Tomcat via IntelliJ. Click the green arrow on the left side.

When the web application completes the booting process, the output console has this output.

Step 4: Access URL

Loading

Got comments or suggestions? We disabled the comments on this site to fight off spammers, but you can still contact us via our Facebook page!.


You Might Also Like