In this post, we will learn how to replace Tomcat with Undertow in Spring Boot with an example.
What is Undertow?
Undertow is the new embeddable web server written in Java. It has great HTTP2 support, it provides blocking and not blocking APIs based in Java NiO. And most importantly, the undertow is lightweight.
Replace the Tomcat dependency with Undertow
To use Undertow instead of tomcat, first, you need to exclude the spring-boot-starter-tomcat from spring-boot-starter-web. Then you should add the undertow starter as shown here.
<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>Code language: HTML, XML (xml)
Once the above change is complete, you can restart your server and see that the server is now Undertow.
If you configured any server.tomcat.* properties in your application config, then you may need to reconfigure them for the Undertow. Otherwise, you have completed replacing tomcat with Undertow in spring boot.
Points to note while switching to Undertow
The first and most important point to note about undertow is that you can’t use JSP with the Undertow. This is not necessarily a problem as most spring boot applications use template engines like thymeleaf or freemarker for their MVC setup.
Undertow is known to using “Direct Buffer” a concept introduced part of Java NiO. With direct buffers, undertow can allocate memory outside heap so that OS can perform IO operations quickly. But without proper JVM allocations, this might backfire if your machine doesn’t have enough RAM.