Configuring Spring Boot embedded Tomcat

In this tutorial you will learn how to configure the default Web Server embedded in Spring Boot

By default, Spring Boot autoconfigure the default Tomcat server for all requests at the default Web Root Context (“/”). The simplest way to configure the embedded Tomcat is by means of properties.

Here is an example application.properties configuration with some common properties:

# Server HTTP port server.port=8080   # Path of the error controller. server.error.path=/error   # Enable the default error page displayed in browsers in case of a server error. server.error.whitelabel.enabled=true   # Value to use for the Server response header (no header is sent if empty) server.server-header=   # Path of the main dispatcher servlet. server.servlet-path=/   # If response compression is enabled. server.compression.enabled=false   # Context path of the application. server.context-path=/myapp  # Display name of the application.  server.display-name=application   # When to include a "stacktrace" attribute. server.error.include-stacktrace=never 

The full list of Properties is contained in the class org.springframework.boot.autoconfigure.web.ServerProperties

Discover programmatically the Web Server port

You can discover the port the server is running on from log output or from the ServletWebServerApplicationContext through its WebServer. Here is an example of it:

public class MyWebIntegrationTests {
  @Autowired ServletWebServerApplicationContext server;
  @LocalServerPort int port;
}

How to disable the embedded Web server

If your classpath contains the necessary bits to start a web server, Spring Boot will automatically start it. To disable this behaviour configure the WebApplicationType in your application.properties, as shown in the following example:

spring.main.web-application-type=none

Configuring Security in the Web Server

SSL can be configured declaratively by setting the various server.ssl.* properties, typically in application.properties or application.yml. The following example shows setting SSL properties in application.properties:

server.port=8443 server.ssl.key-store=classpath:keystore.jks server.ssl.key-store-password=secret server.ssl.key-password=another-secret

If you want to switch to Undertow Web Server, check this tutorial: Configure Spring Boot to use Undertow Web server

If you want to switch to Jetty Web Server, check this tutorial: Configure Spring Boot to use Jetty Server

Found the article helpful? if so please follow us on Socials
Twitter Icon       Facebook Icon       LinkedIn Icon       Mastodon Icon