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

Bitbucket is for the code that takes us to Mars, decodes the human genome, or drives your next car. What will your code do? Get started with Bitbucket today, it's 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 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.

Bitbucket is the Git solution for professional teams who code with a purpose, not just as a hobby. Get started today, it's free.

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.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

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

{{ parent.tldr }}

{{ parent.urlSource.name }}