Ebooks

SpringBootServletInitializer tutorial

SpringBootServletInitializer tutorial shows 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.

pom.xml
src
├───main
│   ├───java
│   │   └───com
│   │       └───zetcode
│   │           │   Application.java
│   │           └───controller
│   │                   MyController.java
│   └───resources
└───test
    └───java

This is the project structure.

pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.zetcode</groupId>
    <artifactId>servletinitializer</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
    </properties>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.1.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>    

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

This is the Maven 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.

com/zetcode/controller/MyController.java
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.

com/zetcode/Application.java
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 tutorial, we have created our first Spring Boot web application deployable from a traditional WAR. You might also be interested in the related tutorials: Spring Boot first web application, Introduction to Spring web applications, Standalone Spring applications, FreeMarker tutorial, Java tutorial, Introduction to Play, Introduction to Spark, or Introduction to Stripes.