SpringBootServletInitializer – Deploy WAR file to Tomcat..!!

SpringBootServletInitializer – Deploy WAR file to Tomcat..!!

Being able to start the application as standalone jar is great, but sometimes it might not be possible to run an app as jar [environment restrictions, company-wide regulations etc] and you have to build a WAR to be deployed into a traditional web/application server. Spring Boot helps us creating WAR using SpringBootServletInitializer.

In this article, we will show you how to deploy a Spring Boot WAR file to the Tomcat servlet container.For Spring Boot WAR deployment, you need to do three steps:

  • Extends SpringBootServletInitializer
  • Marked the embedded servlet container as provided.
  • Update packaging to war

Step 1: Extend from SpringBootServletInitializer

SpringBootServletInitializer is an abstract class implementing WebApplicationInitializer interface , the main abstraction for servlet 3.0+ environments in order to configure the ServletContext programmatically. It Binds Servlet, Filter and ServletContextInitializer beans from the application context to the servlet container.

Create a class which extends SpringBootServletInitializer, overriding it’s configure method. You could as well let your application’s main class to extend SpringBootServletInitializer:

Step 2: Update packaging to ‘war’

Step 3 : Exclude the embedded container from ‘war’

We can add application.properties or YAML file , here I have added YAML file in which I have mentioned the Tomcat related configuration added one place

Now finally execute the spring boot application main class and we see that Tomcat server is up and running and war is deployed automatically on configure the port there is no need to download and install tomcat on local

And finally here the browser output as shown below ... :)







To view or add a comment, sign in

Insights from the community

Others also viewed

Explore topics