SpringBootServletInitializer
last modified July 23, 2023
In this article we show how to deploy a Spring Boot application from a traditional WAR deployment.
The current trend is to deploy Spring Boot application from an executable JAR. (See the Spring Boot first web application for details how to start a simple web application from JAR.)
Spring is a popular Java application framework. Spring Boot is an effort to create stand-alone, production-grade Spring based applications with minimal effort.
SpringBootServletInitializer
SpringBootServletInitializer
is an interface to run
SpringApplication from a traditional WAR deployment. It binds Servlet, Filter
and ServletContextInitializer beans from the application context to the server.
SpringBootServletInitializer example
The application creates a simple Spring Boot RESTful application and packages it into a WAR.
build.gradle ... src ├───main │ ├───java │ │ └───com │ │ └───zetcode │ │ │ Application.java │ │ └───controller │ │ MyController.java │ └───resources └───test └───java
This is the project structure.
plugins { id 'org.springframework.boot' version '3.1.1' id 'io.spring.dependency-management' version '1.1.0' id 'java' } group = 'com.example' version = '0.0.1-SNAPSHOT' sourceCompatibility = '17' repositories { mavenCentral() } dependencies { implementation 'org.springframework.boot:spring-boot-starter-web' }
This is the Gradle build file. The spring-boot-starter-web
is
starter for building web, including RESTful, applications using Spring MVC.
The application is packaged into a WAR file.
package com.zetcode.controller; import org.springframework.http.MediaType; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class MyController { @GetMapping(value = "/", produces = MediaType.TEXT_PLAIN_VALUE) public String index() { return "Hello there"; } }
This is the controller class for the Spring Boot web application. A controller
is decorated with the @Restontroller
annotation.
@GetMapping(value = "/", produces = MediaType.TEXT_PLAIN_VALUE) public String index() { return "Hello there"; }
A GET request to the home page returns a string. The binding is done with
@GetMapping
.
package com.zetcode; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; @SpringBootApplication public class Application extends SpringBootServletInitializer { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
The Application
sets up the Spring Boot application. It extends
from SpringBootServletInitializer
so that it can be deployed as
a WAR.
The application can be run both by deploying the WAR on a Tomcat server and executing it as a self-executable web archive with embedded Tomcat.
In this article we have created our first Spring Boot web application deployable from a traditional WAR.