Exploring spring-boot-starter-undertow in Spring Boot

As a seasoned Spring Framework developer, you might be familiar with the default embedded web server in Spring Boot, which is Tomcat. However, Spring Boot offers flexibility in choosing other web servers like Jetty and Undertow. In this blog post, we’ll delve into the spring-boot-starter-undertow starter, exploring its benefits and providing practical examples to get you started.

Why Choose Undertow?

Undertow is a lightweight, high-performance web server written in Java. It supports both blocking and non-blocking APIs based on Java NIO. Here are some reasons to consider using Undertow:

  • Performance: Undertow is known for its high throughput and low latency.
  • HTTP/2 Support: It has excellent support for HTTP/2, which can improve web application performance.
  • Flexibility: Undertow can be embedded in applications or run as a standalone server.
  • Lightweight: It has a small footprint, making it ideal for microservices.

Setting Up spring-boot-starter-undertow

To switch from Tomcat to Undertow in your Spring Boot application, follow these steps:

  1. Exclude Tomcat Dependency: First, exclude the default Tomcat dependency from spring-boot-starter-web.
  2. Add Undertow Dependency: Then, add the spring-boot-starter-undertow dependency.

Here’s how you can do it in your pom.xml:

<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>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-undertow</artifactId>
</dependency>

For Gradle, you can configure the dependencies as follows:

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.springframework.boot:spring-boot-starter-undertow'
    implementation 'org.springframework.boot:spring-boot-starter-webflux'
    modules {
        module("org.springframework.boot:spring-boot-starter-reactor-netty") {
            replacedBy("org.springframework.boot:spring-boot-starter-undertow", "Use Undertow instead of Reactor Netty")
        }
    }
}

Example Application

Let’s create a simple Spring Boot application to demonstrate the use of Undertow.

  1. Create a Spring Boot Application: Use Spring Initializr to generate a new Spring Boot project with the necessary dependencies.

  2. Application Properties: Configure your application properties if needed. For example, you can set the server port:

server.port=8081
  1. Controller: Create a simple REST controller to handle requests.
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

    @GetMapping("/hello")
    public String sayHello() {
        return "Hello, Undertow!";
    }
}
  1. Run the Application: Run your Spring Boot application and navigate to http://localhost:8081/hello to see the response from your Undertow server.

Points to Note

  • JSP Support: Undertow does not support JSP. If your application relies on JSP, you might need to consider other template engines like Thymeleaf or FreeMarker.
  • Direct Buffers: Undertow uses direct buffers for IO operations, which can improve performance but requires proper JVM memory configuration.

By following these steps, you can easily switch to Undertow and leverage its performance benefits in your Spring Boot applications. Happy coding! 🚀


Feel free to ask if you have any questions or need further assistance with Spring Boot!

Comments

Popular posts from this blog

spring-boot-starter-log4j2

Understanding spring-boot-starter-tomcat in Spring Boot