Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Spring Boot: Changing/Configuring the Default Embedded Server

DZone's Guide to

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.

· Java Zone
Free Resource

Learn how to troubleshoot and diagnose some of the most common performance issues in Java today. Brought to you in partnership with AppDynamics.

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 spring-boot-starter-undertow.

Adding Dependencies

<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

The 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: text/html, text/xml, text/plain, text/css, text/javascript,

application/javascript. The default value for minResponseSize is 2048 bytes.

Other Server Properties

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

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.

Understand the needs and benefits around implementing the right monitoring solution for a growing containerized market. Brought to you in partnership with AppDynamics.

Topics:
spring boot ,java ,configuration ,embedded servers ,tutorial

Published at DZone with permission of Gaurav Rai Mazra, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}