Spring Boot Undertow
last modified July 16, 2023
Spring Boot Undertow tutorial shows how to use Undertow server in a Spring Boot application.
Spring is a popular Java application framework and Spring Boot is an evolution of Spring that helps create stand-alone, production-grade Spring based applications easily.
Undertow
Undertow is a flexible performant web server which provides both blocking and non-blocking API's. It comes from the JBoss project.
Spring Boot Undertow example
By default, Spring Boot uses a Tomcat embedded web server. The following example shows how to use Undertow.
build.gradle ... src ├───main │ ├───java │ │ └───com │ │ └───zetcode │ │ │ Application.java │ │ ├───config │ │ │ AppConfig.java │ │ └───controller │ │ MyController.java │ └───resources │ application.properties └───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.zetcode' version = '0.0.1-SNAPSHOT' sourceCompatibility = '17' repositories { mavenCentral() } dependencies { implementation('org.springframework.boot:spring-boot-starter-web') { exclude group: 'org.springframework.boot', module: 'spring-boot-starter-tomcat' } implementation 'org.springframework.boot:spring-boot-starter-undertow' }
This is the Gradle build file. We explicitly exclude the Tomcat server dependency and include the Undertow dependency.
spring.main.banner-mode=off
In the application.properties
file we have various configuration
settings of a Spring Boot application. With the spring.main.banner-mode
property we turn off the Spring banner.
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 home() { return "Home page"; } }
The home page returns a simple text message.
package com.zetcode; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
The Application
is the entry point which sets up Spring Boot
application.
$ ./gradlew bootRun
We run the application and navigate to localhost:8080
.
... ... io.undertow : starting server: Undertow - 2.3.7.Final ... org.xnio : XNIO version 3.8.8.Final ... org.xnio.nio : XNIO NIO Implementation Version 3.8.8.Final ... org.jboss.threads : JBoss Threads version 3.5.0.Final ... o.s.b.w.e.undertow.UndertowWebServer : Undertow started on port(s) 8080 (http)
In the console we can see the Undertow server starting.
In this article we have shown how use Undertow server in a Spring Boot application.