Spring Boot - Changing and configuring default embedded server

We will start with available servers and then change the embedded server to undertow and then configure the server specific properties.

1. Introduction

Spring Boot supports Tomcat, Undetow and Jetty as embedded servers. Now, we will change and/ or configure the default embedded server and common properties to all the available servers.

In the previous post, we created a web application which uses Apache Tomcat as default embedded server. We will change it to undertow.

Spring Boot provides convenient way of configuring dependencies with its starters. For changing the embedded server, we will user its spring-boot-starter-undertow.

2. Adding dependencies

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-undertow</artifactId>
</dependency>

spring-boot-starter-web comes with Embedded Apache Tomcat. We need to exclude this dependency.

<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>

This is all we need to do to change the embedded server.

3. Generic properties

There are some generic properties which is applicable for every server and some server specific properties that we can tweak to improve the preformance. Let's change some of the server properties.

3.1. Changing the default server port

server.port property is used for configuring the port on our Spring Boot application should run.

3.2. Enabling compression on responses

You can enable to compression on response sent by server and can tweak the mimeTypes, minResponseSize for compression. By default, the compression is disabled. Default property value for mimeTypes is text/html, text/xml, text/plain, text/css, text/javascript,application/javascript. Default property value for minResponseSize is 2048 bytes.

3.3. Other server properties

You can also enable ssl, modify maxHttpPostSize, contextParameters, contextPath and other server related properties. To know more, see org.springframework.boot.autoconfigure.web.ServerProperties class.

3.4. Configuring sever-specific properties

You can also change embedded server specific properties. In our example, we have changed embedded server to Undertow and have tweaked its ioThreads and workerThreads properties.

A sample properties file which have above mentioned properties changes.

server:
port: 8082
undertow:
ioThreads: 15
workerThreads: 150
accesslog:
enabled: true
compression:
enabled: true
mimeTypes: text/xml, text/css, text/html, application/json
minResponseSize: 4096

spring:
application:
name: gauravdotcc-embedded-server-example

I hope this post is informative and helpful. You can see the full example code on Github.



Tags: Spring Framework, Spring Boot, Spring Boot quickstart server configuration, Spring Boot starters

← Back home