Tomcat WAR Deployment - Spring Boot Application

Once we finish our development and testing of a new Spring Boot application, we plan to deploy it on an external tomcat server by generating a WAR file, which is used to deploy the application on external tomcat server.

There are certain changes that we need to make on a Spring Boot application for smooth deployment on external tomcat server, like DEV, QA, etc.,

Here is a list of all such changes for reference.

Changes to pom.xml

Add the packaging type to "war" as shown below.

<packaging>war</packaging>

Add the below dependency to pom.xml file to let the application use external tomcat when deployed on external tomcat server, otherwise use embedded tomcat server.

This helps in run the application in both local (using embedded tomcat) and tomcat server (using the tomcat server on which it is deployed).

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

Changes to class annotated with @SpringBootApplication

We need to extend the application with SpringBootServletInitializer and override the configure method as shown below.

This makes the application use the servlets to resolve the paths exposed by the application. All the paths on the application returns 404 NOT FOUND even aftter successful deployment on external tomcat without this.

package com.example.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 {

	public static void main(String[] args) {
		SpringApplication.run(DemoApplication.class, args);
	}
	
	@Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(DemoApplication.class);
    }

}

Conclusion

Now, we know how to make a Spring Boot application ready for deployment on to an external tomcat server.