Spring Boot Web using Undertow Embedded Servlet Container

Tags: Spring Boot Undertow Embedded Servlet Container Spring Boot Starter Undertow

Introduction

In this Spring Boot tutorial, we learn how to use Undertow as an embedded servlet container in Spring Boot Web application.

Create New Spring Boot Web Project

Open Spring Tool Suite IDE, select menu File > New > Spring Starter Project.

On the New Spring Starter Project popup input new project information as below and click Next.

  • Name: spring-boot-undertow
  • Group: dev.simplesolution
  • Artifact: spring-boot-api-doc
  • Version: 1.0.0
  • Description: Spring Boot using Undertow
  • Package: dev.simplesolution.undertow

Spring Boot Web using Undertow Embedded Servlet Container

On the New Spring Starter Project Dependencies popup choose dependencies as below and click Next.

  • Thymeleaf
  • Spring Web

Spring Boot Web using Undertow Embedded Servlet Container

Keep the information on the next popup as default and click Finish.

Spring Boot Web using Undertow Embedded Servlet Container

What is Undertow?

Undertow is a flexible performant web server written in java, providing both blocking and non-blocking API’s based on NIO.

To have more information about the Undertow servlet container library you can visit the Undertow homepage at undertow.io

Add Spring Boot Starter Undertow to your Spring Boot project

Spring Boot provides a starter for using Undertow as the embedded servlet container named Spring Boot Starter Undertow. We can use Spring Boot Starter Undertow as an alternative to Spring Boot’s default Tomcat servlet container.

To use the Spring Boot Starter Undertow Java library in the Gradle build project, add the following dependency into the build.gradle file. And exclude spring-boot-starter-tomcat module from spring-boot-starter-web.

implementation 'org.springframework.boot:spring-boot-starter-undertow:2.4.3'
implementation ('org.springframework.boot:spring-boot-starter-web') {
	exclude module: 'spring-boot-starter-tomcat'
}

To use the Spring Boot Starter Undertow Java library in the Maven build project, add the following dependency into the pom.xml file. And exclude spring-boot-starter-tomcat module from spring-boot-starter-web.

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-undertow</artifactId>
  <version>2.4.3</version>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <exclusions>
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </exclusion>
    </exclusions>
</dependency>

Implement Sample Controller and View

In the package dev.simplesolution.undertow, create a new class named IndexController.

package dev.simplesolution.undertow;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class IndexController {
	
	@GetMapping("/")
	public String index() {
		return "index";
	}

}

Create new HTML view file at \src\main\resources\templates\index.html

<!DOCTYPE html>
<html>
	<body>
		<h1>Web App using Undertow Servlet Container</h1>
	</body>
</html>

Run the application, we can see the log messages showing the application use Undertow server.

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v2.4.3)

2021-03-02 00:17:38.744  INFO 19148 --- [           main] d.s.u.SpringBootUndertowApplication      : Starting SpringBootUndertowApplication using Java 1.8.0_231 on ss with PID 19148 (D:\SimpleSolution\spring-boot-undertow\bin\main started by SS in D:\SimpleSolution\spring-boot-undertow)
2021-03-02 00:17:38.746  INFO 19148 --- [           main] d.s.u.SpringBootUndertowApplication      : No active profile set, falling back to default profiles: default
2021-03-02 00:17:39.204  WARN 19148 --- [           main] io.undertow.websockets.jsr               : UT026010: Buffer pool was not set on WebSocketDeploymentInfo, the default pool will be used
2021-03-02 00:17:39.216  INFO 19148 --- [           main] io.undertow.servlet                      : Initializing Spring embedded WebApplicationContext
2021-03-02 00:17:39.217  INFO 19148 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 436 ms
2021-03-02 00:17:39.327  INFO 19148 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2021-03-02 00:17:39.366  INFO 19148 --- [           main] o.s.b.a.w.s.WelcomePageHandlerMapping    : Adding welcome page template: index
2021-03-02 00:17:39.434  INFO 19148 --- [           main] io.undertow                              : starting server: Undertow - 2.2.4.Final
2021-03-02 00:17:39.438  INFO 19148 --- [           main] org.xnio                                 : XNIO version 3.8.0.Final
2021-03-02 00:17:39.442  INFO 19148 --- [           main] org.xnio.nio                             : XNIO NIO Implementation Version 3.8.0.Final
2021-03-02 00:17:39.462  INFO 19148 --- [           main] org.jboss.threads                        : JBoss Threads version 3.1.0.Final
2021-03-02 00:17:39.498  INFO 19148 --- [           main] o.s.b.w.e.undertow.UndertowWebServer     : Undertow started on port(s) 8080 (http)
2021-03-02 00:17:39.504  INFO 19148 --- [           main] d.s.u.SpringBootUndertowApplication      : Started SpringBootUndertowApplication in 0.973 seconds (JVM running for 1.865)

Open the link http://localhost:8080/ on your browser to see the web page as below.

Spring Boot Web using Undertow Embedded Servlet Container

Final Application Source Code

At this step we have the complete Spring Boot web project with code structure as below.

Spring Boot Web using Undertow Embedded Servlet Container

Download Source Code

The source code in this article can be found at: github.com/simplesolutiondev/spring-boot-undertow

or clone at:

git clone https://github.com/simplesolutiondev/spring-boot-undertow.git

or download at:

Download Source Code

Happy Coding 😊