Configure embedded Undertow server in Spring Boot
Page content
In this quick tutorial, we’ll configure embedded Undertow server by replacing it with default Tomcat server in Spring Boot web application.
Add Undertow Dependency
We need to do two things here:-
- Exclude default dependency
spring-boot-starter-tomcat
added inspring-boot-start-web
- Add
spring-boot-starter-undertow
dependency.
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>
build.gradle
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'
}
That’s it. You have replaced tomcat with Undertow server.
Application Startup Logs
When you start spring boot application, You will in the logs that Undertow is serving your web application now:-
INFO c.e.demo.SpringBootDemoApplication : Starting SpringBootDemoApplication using Java 11.0.10 on Ashishs-MBP with PID 5166 (/Users/ashl/IdeaProjects/springboot-examples/springboot-config/build/classes/java/main started by ashl in /Users/ashl/IdeaProjects/springboot-examples/springboot-config)
DEBUG c.e.demo.SpringBootDemoApplication : Running with Spring Boot v2.5.0, Spring v5.3.7
INFO c.e.demo.SpringBootDemoApplication : No active profile set, falling back to default profiles: default
WARN io.undertow.websockets.jsr : UT026010: Buffer pool was not set on WebSocketDeploymentInfo, the default pool will be used
INFO io.undertow.servlet : Initializing Spring embedded WebApplicationContext
INFO w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 753 ms
INFO io.undertow : starting server: Undertow - 2.2.7.Final
INFO org.xnio : XNIO version 3.8.0.Final
INFO org.xnio.nio : XNIO NIO Implementation Version 3.8.0.Final
INFO org.jboss.threads : JBoss Threads version 3.1.0.Final
INFO o.s.b.w.e.undertow.UndertowWebServer : Undertow started on port(s) 8080 (http)
INFO c.e.demo.SpringBootDemoApplication : Started SpringBootDemoApplication in 1.858 seconds (JVM running for 2.21)
Add Spring Boot Undertow Configuration
Spring boot also provides Undertow specific configuration which you can configure from application.yml
or application.properties
file.
application.yml
server:
undertow:
allow-encoded-slash: false # Whether the server should decode percent encoded slash characters. Enabling encoded slashes can have security implications due to different servers interpreting the slash differently. Only enable this if you have a legacy application that requires it.
always-set-keep-alive: true # Whether the 'Connection: keep-alive' header should be added to all responses, even if not required by the HTTP specification.
buffer-size: # Size of each buffer. The default is derived from the maximum amount of memory that is available to the JVM.
decode-url: true # Whether the URL should be decoded. When disabled, percent-encoded characters in the URL will be left as-is.
direct-buffers: # Whether to allocate buffers outside the Java heap. The default is derived from the maximum amount of memory that is available to the JVM.
eager-filter-init: true # Whether servlet filters should be initialized on startup.
max-cookies: 200 # Maximum number of cookies that are allowed. This limit exists to prevent hash collision based DOS attacks.
max-headers: 12 # Maximum number of headers that are allowed. This limit exists to prevent hash collision based DOS attacks.
max-parameters: # Maximum number of query or path parameters that are allowed. This limit exists to prevent hash collision based DOS attacks.
max-http-post-size: -1B # Maximum size of the HTTP post content. When the value is -1, the default, the size is unlimited.
no-request-timeout: # Amount of time a connection can sit idle without processing a request, before it is closed by the server.
preserve-path-on-forward: false # Whether to preserve the path of a request when it is forwarded.
url-charset: UTF-8 # Charset used to decode URLs
accesslog:
enabled: false # Whether to enable the access log
dir: # Undertow access log directory
pattern: common # Format pattern for access logs
rotate: true # Whether to enable access log rotation
prefix: access_log. # Log file name prefix.
suffix: log # Log file name suffix
threads:
io: # Number of I/O threads to create for the worker. The default is derived from the number of available processors.
worker: # Number of worker threads. The default is 8 times the number of I/O threads.
You can refer to the Spring Boot official documentation for full list of configuration.