Configure Spring Boot to use Undertow Web server

By default, Spring boot uses Tomcat as embedded Web server. There are, however, other available Web servers in case you need some specific features. In this tutorial we will learn how to use Undertow as Web Server.

Add spring-boot-starter-undertow dependency

You will need to update pom.xml and add dependency for spring-boot-starter-undertow. Also, you will need to exclude default added spring-boot-starter-tomcat dependency as follows:

<?xml version="1.0" encoding="UTF-8"?><project>
   <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>
</project>

If you are using Gradle as build tool, the same result can be achieved using:

configurations { 
    compile.exclude module: "spring-boot-starter-tomcat" 
} 
dependencies { 
    compile("org.springframework.boot:spring-boot-starter-web:2.1.0.RELEASE") 
    compile("org.springframework.boot:spring-boot-starter-undertow:2.1.0.RELEASE") 
}

Configuring Undertow Programmatically

You can also start the embedded Undertow Web server programmatically through the UndertowEmbeddedServletContainerFactory:

  @Bean
  public UndertowEmbeddedServletContainerFactory embeddedServletContainerFactory() {
    UndertowEmbeddedServletContainerFactory factory = new UndertowEmbeddedServletContainerFactory();
    factory.addBuilderCustomizers(
        new UndertowBuilderCustomizer() {
          @Override
          public void customize(io.undertow.Undertow.Builder builder) {
            builder.addHttpListener(8080, "0.0.0.0");
          }
        });
    return factory;
  }

 As for the default Web Server, you can configure custom settings for Undertow through the application.properties file:
server.undertow.accesslog.enabled=true 
server.undertow.accesslog.dir=target/logs 
server.undertow.accesslog.pattern=combined 
server.compression.enabled=true 
server.compression.min-response-size=1