Spring Boot embeds a web server — Tomcat by default — directly in the application jar. This eliminates the need to deploy to an external container, but it also means server configuration happens through Spring Boot properties and programmatic customizers rather than server-specific XML files. This guide covers the most common embedded server configuration tasks.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/spring-projects/spring-boot/llms.txt
Use this file to discover all available pages before exploring further.
Changing the server port
The default HTTP port is8080. Override it with server.port in application.properties or application.yaml:
SERVER_PORT) or a JVM system property (-Dserver.port=9090).
To disable the HTTP endpoint entirely (useful in some test scenarios) while still creating a WebApplicationContext:
Using a random port
To let the OS assign a free port (avoiding clashes in parallel test runs), setserver.port=0:
@LocalServerPort:
@LocalServerPort is a meta-annotation for @Value("${local.server.port}"). Do not inject it in regular application code — the value is only available after the container starts, which is after most application beans initialize.Configuring HTTPS
Configure HTTPS declaratively by settingserver.ssl.* properties. Spring Boot supports Java KeyStore files and PEM-encoded certificate files.
- Java KeyStore (JKS)
- PEM files
- SSL bundles
Switching the embedded server
Spring Boot supports Tomcat (default), Jetty, and Undertow. To switch servers, exclude the default starter and add the replacement.Tomcat to Jetty
- Maven
- Gradle
spring-boot-starter-reactor-netty from spring-boot-starter-webflux and add the replacement starter.
For reactive stack applications, the
spring-boot-starter-webflux includes Reactor Netty by default. You can use spring-boot-starter-tomcat or spring-boot-starter-jetty instead.Configuring thread pools and connection settings
Useserver.tomcat.* properties to tune Tomcat’s thread pool and connection handling:
server.jetty.*:
Enabling HTTP/2
Setserver.http2.enabled=true to enable HTTP/2. Both h2 (over TLS) and h2c (over TCP, cleartext) are supported.
HTTP/2 with Tomcat
HTTP/2 with Tomcat
Spring Boot ships with Tomcat 11.0.x, which supports both
h2c and h2 out of the box. No additional dependencies are required. To use h2, SSL must also be enabled. To use h2c without TLS (for example, behind a TLS-terminating proxy), enable HTTP/2 and leave SSL disabled.Optionally, libtcnative can be used for h2 support if installed on the host OS:HTTP/2 with Jetty
HTTP/2 with Jetty
Jetty requires the additional dependency
org.eclipse.jetty.http2:jetty-http2-server.For h2 (over TLS), also add one of:org.eclipse.jetty:jetty-alpn-java-server— uses the JDK’s built-in ALPN supportorg.eclipse.jetty:jetty-alpn-conscrypt-server— uses the Conscrypt library
h2c.HTTP/2 with Reactor Netty (WebFlux)
HTTP/2 with Reactor Netty (WebFlux)
Reactor Netty supports both
h2 and h2c out of the box. For optimal performance with native libraries, add io.netty:netty-tcnative-boringssl-static.Enabling response compression
HTTP response compression is supported by Tomcat, Jetty, and Reactor Netty. Enable it inapplication.properties:
- Responses must be at least 2048 bytes to be compressed.
- Only
text/html,text/xml,text/plain,text/css,text/javascript,application/javascript,application/json, andapplication/xmlcontent types are compressed.
additional-mime-types instead of overriding mime-types.
Configuring proxy and forwarded headers
When running behind a reverse proxy or load balancer, the original request information (host, port, scheme) may differ from what the application sees. Configureserver.forward-headers-strategy to handle forwarded headers:
NATIVE: Instructs the web server itself to handleX-Forwarded-*headers natively. Works forX-Forwarded-ForandX-Forwarded-Proto.FRAMEWORK: Activates Spring’sForwardedHeaderFilter(servlet) orForwardedHeaderTransformer(reactive). Use whenNATIVEis insufficient.
On supported cloud platforms (Cloud Foundry, Kubernetes, etc.),
server.forward-headers-strategy defaults to NATIVE. On all other platforms, it defaults to NONE.Graceful shutdown
To enable graceful shutdown, configureserver.shutdown=graceful. When a shutdown signal is received, the server stops accepting new requests and waits for in-flight requests to complete before the context is closed.
Programmatic server customization
For cases not covered byapplication.properties, use WebServerFactoryCustomizer to access the server factory directly.
The following example customizes Tomcat using the servlet stack:
| Server | Servlet stack | Reactive stack |
|---|---|---|
| Tomcat | TomcatServletWebServerFactory | TomcatReactiveWebServerFactory |
| Jetty | JettyServletWebServerFactory | JettyReactiveWebServerFactory |
| Reactor Netty | N/A | NettyReactiveWebServerFactory |
Spring Boot’s own auto-configured
WebServerFactoryCustomizer beans have order 0 and run before any user-defined customizers unless you set an explicit order on yours.