A WAR file packages a Spring Boot web application for deployment to a traditional Java application server. Unlike an executable JAR, it includes the servlet initialization needed for the server to start Spring MVC. section 18.1 section 18.4
Choose the archive format according to the runtime environment:
- Use a WAR when deploying to Tomcat, WebSphere, WebLogic, or another traditional Java application server. section 18.1
- Prefer an executable JAR for a Platform as a Service cloud, because its format is simpler than WAR. section 18.1
- Use a WAR when JSP requires deployment into a traditional servlet container; executable JAR packaging cannot satisfy that requirement. section 2.5
A Spring Boot WAR needs a servlet initializer so the application server can configure Spring’s DispatcherServlet. SpringBootServletInitializer supplies this integration and can bind application filters, servlets, and servlet context initializers. section 18.4
Listing 18.1 Enabling Spring web applications via Java
package tacos;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.web.SpringBootServletInitializer;
public class TacoCloudServletInitializer
extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(
SpringApplicationBuilder builder) {
return builder.sources(TacoCloudApplication.class);
}
}
This initializer listing shows the recommended Java configuration: extend SpringBootServletInitializer and return the main application class from configure(). section 18.4
Building and running a WAR
Configure Maven with <packaging>war</packaging> or apply Gradle’s war plugin, then build with mvnw package or gradlew build. The resulting WAR can also be launched with java -jar when supported by the Spring Boot setup. section 18.4
