Spring Boot: Changing/Configuring the Default Embedded Server
In this post we take a look at how to set and configure the default embedded web server used in your Spring Boot application.
Join the DZone community and get the full member experience.Join For Free
In a previous post, we created a web-based Spring Boot application that uses Embedded Tomcat as the default server running on the default port,
8080. Spring Boot supports Tomcat, Undertow, and Jetty as embedded servers. Now, we will change and/or configure the default embedded server and common properties to all the available servers.
Spring Boot provides a convenient way of configuring dependencies with its starters. For changing the embedded server, we will user its
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-undertow</artifactId> </dependency>
spring-boot-starter-web comes with Embedded 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. There are some generic properties that are applicable for every server, and some server-specific properties that we can tweak to improve performance. Let's change some of the server properties.
Changing the Default Server Port
server.port property is used for configuring the port on which our Spring Boot application should run.
Enabling Compression on Responses
You can enable compression on responses sent by the server and can tweak the mimeTypes, and minResponseSize for compression. By default, compression is disabled. The default property value for mimeTypes is:
Other Server Properties
You can also enable SSL, modify maxHttpPostSize, contextParameters, contextPath , and other server-related properties. To know more, see the
Configuring Server-Specific Properties
You can also change embedded server-specific properties. In our example, we changed the embedded server to Undertow and tweaked its ioThreads and workerThreads properties.
A sample properties file, which have the above property changes, would look like so:
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: gaurav-bytes-embedded-server-example
I hope this article is informative and helpful. You can grab the full example code on Github.
Published at DZone with permission of Gaurav Rai Mazra, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.