Ebooks

Spring Boot WebApplicationType

Spring Boot WebApplicationType tutorial presents various types of web applications in a Spring Boot application. The example shows how to set the WebApplicationType.

Spring Boot is a popular application framework for creating enterprise application in Java, Kotlin, or Groovy.

WebApplicationType

The WebApplicationType is an enumeration of possible types of web applications. There are three possible values:

Spring Boot example

In the following application, we define the web application type of a Spring Boot application.

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

This is the project structure of the Spring Boot application.

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>SpringBootWebOff</artifactId>
    <version>1.0-SNAPSHOT</version>

    <packaging>jar</packaging>

    <properties>
        <java.version>13</java.version>
    </properties>

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

    <dependencies>

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

    <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>

In the Maven pom.xml file, we have dependencies for a classic servlet and reactive web application.

com/zetcode/Application.java
package com.zetcode;

import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.ServerResponse;

import static org.springframework.web.reactive.function.BodyInserters.fromValue;
import static org.springframework.web.reactive.function.server.RequestPredicates.GET;
import static org.springframework.web.reactive.function.server.RouterFunctions.route;
import static org.springframework.web.reactive.function.server.ServerResponse.ok;

@SpringBootApplication
public class Application {

    public static void main(String[] args) {

        new SpringApplicationBuilder(Application.class)
                .web(WebApplicationType.SERVLET)
                .run(args);
    }
}

@RestController
class MyController {

    @GetMapping("/")
    public String hello() {

        return "Home page";
    }
}

@Configuration
class MyRoutes {

    @Bean
    RouterFunction<ServerResponse> about() {
        return route(GET("/about"), request -> ok().body(fromValue("About page")));
    }
}

@Component
class MyRunner implements CommandLineRunner {

    @Override
    public void run(String... args) throws Exception {

        System.out.println("Hello there!");
    }
}

In the Application, we define the Spring Boot application and set up a classic web rest point, a reactive route and a commandline runner.

new SpringApplicationBuilder(Application.class)
    .web(WebApplicationType.SERVLET)
    .run(args);

We define the web application type using the SpringApplicationBuilder. For the WebApplicationType.SERVLET, the reactive route is not available.

$ mvn spring-boot:run

We start the application.

$ curl localhost:8080/
Home page

The classic servlet rest point is active.

In this tutorial, we have worked with a Spring Boot WebApplicationType.

List Spring Boot tutorials.