Running spring-boot application on tomcat servlet container

In this tutorial, we will deploy the spring-boot application on tomcat servlet container.

Deployment Procedure

We need to add a property in pom.xml file to get the project packaging as WAR. The generated WAR will be deployed in tomcat.


Observe the property of <packaging>war</packaging> in the above pom.xml file that allows us to generate the WAR file under target folder. Application.java needs to extend the class SpringBootServletInitializer
Right-click on the project, click on Run As followed by Maven install to get the project build in WAR.

In the above figure we can see, the WAR generated and is to be deployed under webapps folder of tomcat installation. Spring Application beans are usually managed by ApplicationContext as discussed earlier but in order to initialize the ServletContext for a spring application while deploying in Tomcat it is required to extend SpringBootServletInitializer.

@SpringBootApplication
public class Application extends SpringBootServletInitializer{

        //main method body

}

In case, tomcat is not installed on the machine, download from https://tomcat.apache.org/download-80.cgi . After unzipping the zip folder the installation directory will appear as follows


Place the WAR (generated in the target folder) in the web apps (Notice the above figure 7.3). Now navigate to the bin folder and open cmd. In order to start the Tomcat, it is essential to make sure that the environment variables are properly set which we discussed in the earlier tutorials.

The environment variables are set according to the installation path of JAVA. Deploy the WAR file in the web apps directory of Tomcat installation. Run the below command to start the server. Tomcat will be started after prompting a separate prompt as shown in figure 7.6.



We can test the application deployed on tomcat using postman as follows.

In the above picture, notice the URL which has the WAR name deployed in the tomcat and the status returned was 200 OK. This is the overall procedure for WAR deployment in tomcat. We have been launching the application from IDE using embedded tomcat so far. Let us review the Application launch again from the IDE as it is relevant to this topic.

Right Click on Project------>Run As------>Java Application

The above figure displays the application console and was launched from the IDE.

Conclusion: In this tutorial, we have discussed about the spring-boot application WAR deployment to a servlet-container and also the launch from IDE.