Spring Boot Embedded Tomcat Server Configuration

In this Spring boot tutorial, we will learn to modify the default configuration for embedded Tomcat server by overriding respective properties in application.properties file.

1. Introduction

A default Spring boot web application contains the embedded Tomcat container as a transitive dependency. In other words, the Spring starter spring-boot-starter-web transitively pulls the spring-boot-starter-tomcat dependency.

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-web</artifactId>
</dependency>
[INFO] com.howtodoinjava:rest-api-crud-example:jar:1.0-SNAPSHOT
[INFO] +- org.springframework.boot:spring-boot-starter-web:jar:3.1.2:compile
[INFO] |  +- org.springframework.boot:spring-boot-starter:jar:3.1.2:compile
...
...
[INFO] |  +- org.springframework.boot:spring-boot-starter-tomcat:jar:3.1.2:compile
[INFO] |  |  +- org.apache.tomcat.embed:tomcat-embed-core:jar:10.1.11:compile
[INFO] |  |  \- org.apache.tomcat.embed:tomcat-embed-websocket:jar:10.1.11:compile
...
...

2. Overriding Embedded Tomcat Configuration using Properties

The default Tomcat server comes with some pre-configured default behavior that we can customize with Tomcat-specific properties.

2.1. Server Port and LocalHost Address

server.address=my_custom_ip
server.port=80
  • server.port – Server HTTP port. 8080 is the default value.
  • server.address – Network address to which the server should bind. Default value is 0.0.0.0 which allows connection via all IPv4 addresses.

2.2. Remote Connection Properties

server.connection-timeout=10s
server.max-http-header-size=8KB

server.tomcat.accept-count=100
server.tomcat.max-connections=10000
server.tomcat.max-threads=200
server.tomcat.min-spare-threads=10
server.tomcat.max-swallow-size=2MB
server.tomcat.max-http-post-size=2MB
  • server.connection-timeout – Time that connectors wait for another HTTP request before closing the connection. When not set, the connector’s container-specific default is used. Use a value of -1 to indicate infinite timeout.
  • server.max-http-header-size – Maximum size of the HTTP message header.
  • server.tomcat.accept-count – Maximum queue length for incoming connection requests when all possible request processing threads are in use.
  • server.tomcat.max-connections – Maximum number of connections that the server accepts and processes at any given time.
  • server.tomcat.max-threads – Maximum amount of worker threads in server under top load. In other words, the maximum number of simultaneous requests that can be handled.
  • server.tomcat.min-spare-threads – The minimum number of threads always kept running. This includes both active and idle threads.
  • server.tomcat.max-swallow-size – The maximum number of request body bytes (excluding transfer encoding overhead) that will be swallowed by Tomcat for an aborted upload. An aborted upload is when Tomcat knows that the request body is going to be ignored but the client still sends it.
    If Tomcat does not swallow the body the client is unlikely to see the response. If not specified the default of 2097152 (2 megabytes) will be used. A value of less than zero indicates that no limit should be enforced.
  • server.tomcat.max-http-post-size – Maximum size of the HTTP post content.

2.3. Error Handling

server.error.include-exception=false
server.error.include-stacktrace=never
server.error.path=/error
server.error.whitelabel.enabled=true
  • server.error.include-exception – Include the “exception” attribute while rendering the error view. Default value is false.
  • server.error.include-stacktrace – Include the “stacktrace” attribute while rendering the error view. Default value is never.
  • server.error.path – Default whitelabel error page path URL.
  • server.error.whitelabel.enabled – Whether to enable the default error page displayed in browsers in case of a server error.

2.4. HTTPS or SSL Configuration

server.ssl.enabled=true
server.ssl.protocol=TLS1.2

server.ssl.key-alias=tomcat
server.ssl.key-store=keystore-path
server.ssl.key-store-type=keystore-type
server.ssl.key-store-provider=provider
server.ssl.key-store-password=some-password

server.ssl.trust-store=store-path
server.ssl.trust-store-type=JKS
server.ssl.trust-store-provider=provider
server.ssl.trust-store-password=some-password
  • server.ssl.enabled – Use this attribute to enable SSL traffic on a connector. To turn on SSL handshake/encryption/decryption on a connector set this value to true.
  • server.ssl.protocol – SSL protocol to use. the default is TLS. Pick a value from this list.
  • server.ssl.key-alias – The alias used for the server key and certificate in the keystore. If not specified, the first key read from the keystore will be used.
  • server.ssl.key-store – The pathname of the keystore file where you have stored the server certificate to be loaded.
  • server.ssl.key-store-type – The type of keystore file to be used for the server certificate. If not specified, the default value is “JKS”.
  • server.ssl.key-store-provider – The name of the keystore provider to be used for the server certificate. If not specified, the list of registered providers is traversed in preference order and the first provider that supports the keystoreType is used.
  • server.ssl.key-store-password – The password used to access the specified keystore file. The default value is the value of the keyPass attribute.
  • server.ssl.trust-store – The trust store file to use to validate client certificates.
  • server.ssl.trust-store-type – The type of key store used for the trust store. If that property is not specified, the value of keystore type is used as the default.
  • server.ssl.trust-store-provider – The name of the trust-store provider to be used for the server certificate. If that property is null, the value of keystore provider is used as the default.
  • server.ssl.trust-store-password – The password to access the trust store. If that property is empty, no trust store password will be configured.

2.5. HTTP Access Logging

server.tomcat.accesslog.enabled=true
server.tomcat.accesslog.directory=logs
server.tomcat.accesslog.file-date-format=yyyy-MM-dd
server.tomcat.accesslog.prefix=access_log
server.tomcat.accesslog.suffix=.log
server.tomcat.accesslog.rotate=true
  • server.tomcat.accesslog.enabled – Enable access logging or not.
  • server.tomcat.accesslog.directory – Directory in which log files are created. Can be absolute or relative to the Tomcat base dir.
  • server.tomcat.accesslog.file-date-format – Date format to place in the log file name.
  • server.tomcat.accesslog.prefix – Log file name prefix.
  • server.tomcat.accesslog.suffix – Log file name suffix.
  • server.tomcat.accesslog.rotate – Whether to enable access log rotation.

3. Java Configuration

We can customize the Tomcat server by customizing the TomcatServletWebServerFactory bean as well.

@Component
public class TomcatCustomizer implements WebServerFactoryCustomizer<TomcatServletWebServerFactory> {

    @Override
    public void customize(TomcatServletWebServerFactory factory) {

        factory.setContextPath("");
        factory.setPort(8080);
    }
}

4. Conclusion

This Spring boot tutorial discussed configuring the embedded Tomcat configuration using the properties and Java configurations.

Drop me your questions in the comments related to any Tomcat configuration discussed above.

Happy Learning !!

Comments

Subscribe
Notify of
guest
7 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.

Our Blogs

REST API Tutorial

Dark Mode

Dark Mode