Spring Boot Deploy WAR file example

The Spring Boot Deploy WAR file example will show you how to deploy a Spring Boot WAR file to the Tomcat servlet container. Actually, Spring Boot make a application starts as standalone jar is great, but sometimes it might not be possible to run an application as jar and we have to build a WAR file. Fortunately, Spring Boot helps us creating WAR using SpringBootServletInitializer.

Other interesting posts you may like

Let’s begin:

In this Spring Boot Deploy WAR file example, we will customize the sources code from the Spring Boot Thymeleaf Ajax Example

Customize SpringBootApplication

Our classic SpringBootApplication class in case of Spring Boot JAR deployment

Customize our @SpringBootApplication class by extending SpringBootServletInitializer and overridding the configure method. So we update the SpringBootWebApplication class above to support war deployment like this

Update packaging to war

Packaging to war file

Our application needs to use the Tomcat libraries at runtime, the we should append the below dependency in pom.xml file and set scope of the dependency to provided.

rename war file to javabycode.war, Tomcat take the war file name as the context path

Case study: Spring Boot WAR and Tomcat Deployment
Take the sources code from the Spring Boot Thymeleaf Ajax Example, update it and deploy to Tomcat manually.

Step 1: Update pom.xml like this

Step 2: Update the SpringBootWebApplication class like below

Step 3: Deploy WAR file
Copy our war file named javabycode.war to the Tomcat webapps and start Tomcat manually. Then access the address http://localhost:8080/javabycode and see the result

Spring Boot Deploy WAR file example

That’s all on the Spring Boot Deploy WAR file example.

References
Spring Boot – Traditional deployment


Spring Boot Thymeleaf Ajax Example

Download complete source code, click link below

spring-boot-thymeleaf-ajax-example-war-deployment.rar (35 downloads)

Leave a Comment

*

Please share it if you found this useful
Hide Buttons