Using Undertow as Embedded Server in Spring Boot

For a Spring Boot Application, you can generate an application jar that contains Embedded Tomcat or Undertow or jetty. You can run a web application as a normal Java application! The advantage of this is you don't need the server pre-installed in the deployment environment.

Below we are showing a simple Spring Boot application example using Undertow as Embedded Server.

Undertow is a flexible performant web server, providing both blocking and non-blocking APIs based on NIO. It has a composition based architecture that allows you to build a web server by combining small single purpose handlers. The gives you the flexibility to choose between a full Java EE servlet container, or a low-level non-blocking handler, to anything in between.
The main advantages of undertow are HTTP/2 Support, HTTP Upgrade Support, Web Socket Support, Servlet 4.0, Embeddable, Flexible.

1. Maven/Dependency Management

Remove the existing tomcat dependency on spring-boot-starter-web and add undertow dependency
<?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
https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.1.RELEASE</version>
</parent>
<groupId>com.knowledgefactory</groupId>
<artifactId>springboot-embeddedserver-undertow</artifactId>
<packaging>jar</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>springboot-embeddedserver-undertow</name>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!-- spring mvc, rest -->
<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>
<!-- embedded undertow -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-undertow</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<url>www.knowledgefactory.net</url>
</project>

2. Test Controller

package com.knowledgefactory.knowledgefactorydemo;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class Controller {
@RequestMapping(value = "/hello", method = RequestMethod.GET)
public ResponseEntity<String> listAllUsers() {
return new ResponseEntity<String>
("Greetings from knowledgefactory",
HttpStatus.OK);
}
}

3. Spring Boot -Main Class

package com.knowledgefactory.knowledgefactorydemo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class KnowledgefactorydemoApplication {
public static void main(String[] args) {
SpringApplication.
run(KnowledgefactorydemoApplication.class, args);
}
}

4. Run

$ mvn spring-boot:run

5. Test


Popular posts from this blog

Learn Java 8 streams with an example - print odd/even numbers from Array and List

Java Stream API - How to convert List of objects to another List of objects using Java streams?

Registration and Login with Spring Boot + Spring Security + Thymeleaf

Java, Spring Boot Mini Project - Library Management System - Download

ReactJS, Spring Boot JWT Authentication Example

Top 5 Java ORM tools - 2024

Java - Blowfish Encryption and decryption Example

Spring boot video streaming example-HTML5

Google Cloud Storage + Spring Boot - File Upload, Download, and Delete