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
1 2 3 4 5 6 7 8 9 10 11 12 |
package com.javabycode; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class SpringBootWebApplication { public static void main(String[] args) throws Exception { SpringApplication.run(SpringBootWebApplication.class, args); } } |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.web.support.SpringBootServletInitializer; @SpringBootApplication public class SpringBootWebApplication extends SpringBootServletInitializer { @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(SpringBootWebApplication.class); } public static void main(String[] args) throws Exception { SpringApplication.run(SpringBootWebApplication.class, args); } } |
Update packaging to war
Packaging to war file
1 |
<packaging>war</packaging> |
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.
1 2 3 4 5 6 |
<!-- marked the embedded servlet container as provided --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency> |
rename war file to javabycode.war, Tomcat take the war file name as the context path
1 |
<finalName>javabycode</finalName> |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.javabycode</groupId> <artifactId>spring-boot-thymeleaf-ajax-example</artifactId> <packaging>war</packaging> <version>1.0</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.2.RELEASE</version> <relativePath></relativePath> </parent> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency> <!-- hot swapping, enable live reload --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.webjars</groupId> <artifactId>jquery</artifactId> <version>2.2.4</version> </dependency> <dependency> <groupId>org.webjars</groupId> <artifactId>bootstrap</artifactId> <version>3.3.7</version> </dependency> </dependencies> <build> <finalName>javabycode</finalName> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project> |
Step 2: Update the SpringBootWebApplication class like below
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
package com.javabycode; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.web.support.SpringBootServletInitializer; @SpringBootApplication public class SpringBootWebApplication extends SpringBootServletInitializer{ @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application){ return application.sources(SpringBootWebApplication.class); } public static void main(String[] args) throws Exception { SpringApplication.run(SpringBootWebApplication.class, args); } } |
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
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 (306 downloads)