0
# Embedded Server
1
2
Configure and customize the embedded web server (Tomcat, Jetty, or Undertow) for Spring Boot applications.
3
4
## Capabilities
5
6
### Server Selection
7
8
Choose and configure different embedded servers.
9
10
```gradle { .api }
11
// Default: Tomcat (included in spring-boot-starter-web)
12
implementation 'org.springframework.boot:spring-boot-starter-web'
13
14
// Switch to Jetty
15
implementation('org.springframework.boot:spring-boot-starter-web') {
16
exclude group: 'org.springframework.boot', module: 'spring-boot-starter-tomcat'
17
}
18
implementation 'org.springframework.boot:spring-boot-starter-jetty'
19
20
// Switch to Undertow
21
implementation('org.springframework.boot:spring-boot-starter-web') {
22
exclude group: 'org.springframework.boot', module: 'spring-boot-starter-tomcat'
23
}
24
implementation 'org.springframework.boot:spring-boot-starter-undertow'
25
```
26
27
### Server Factory Customization
28
29
Programmatically customize the embedded server factory.
30
31
```java { .api }
32
/**
33
* Tomcat server customization
34
*/
35
@Bean
36
public WebServerFactoryCustomizer<TomcatServletWebServerFactory> tomcatCustomizer() {
37
return factory -> {
38
factory.setPort(9090);
39
factory.setContextPath("/api");
40
factory.setDisplayName("My Application");
41
42
// Connector customization
43
factory.addConnectorCustomizers(connector -> {
44
connector.setPort(8443);
45
connector.setScheme("https");
46
connector.setSecure(true);
47
connector.setMaxPostSize(50 * 1024 * 1024); // 50MB
48
});
49
50
// Additional customization
51
factory.addAdditionalTomcatConnectors(createSslConnector());
52
};
53
}
54
55
/**
56
* Jetty server customization
57
*/
58
@Bean
59
public WebServerFactoryCustomizer<JettyServletWebServerFactory> jettyCustomizer() {
60
return factory -> {
61
factory.setPort(8081);
62
factory.setContextPath("/app");
63
factory.addServerCustomizers(server -> {
64
// Jetty server customization
65
server.setStopTimeout(5000);
66
});
67
};
68
}
69
70
/**
71
* Undertow server customization
72
*/
73
@Bean
74
public WebServerFactoryCustomizer<UndertowServletWebServerFactory> undertowCustomizer() {
75
return factory -> {
76
factory.setPort(8082);
77
factory.addBuilderCustomizers(builder -> {
78
builder.addHttpListener(8080, "0.0.0.0");
79
builder.setBufferSize(1024);
80
builder.setDirectBuffers(true);
81
});
82
};
83
}
84
```
85
86
### SSL/TLS Configuration
87
88
Configure SSL/TLS for secure connections.
89
90
```java { .api }
91
/**
92
* SSL configuration via properties
93
*/
94
@ConfigurationProperties("server.ssl")
95
public class SslProperties {
96
private boolean enabled = false;
97
private String keyStore;
98
private String keyStorePassword;
99
private String keyStoreType = "JKS";
100
private String keyAlias;
101
private String trustStore;
102
private String trustStorePassword;
103
private String protocol = "TLS";
104
private String[] enabledProtocols;
105
private String[] ciphers;
106
private ClientAuth clientAuth = ClientAuth.NONE;
107
108
public enum ClientAuth { NONE, WANT, NEED }
109
}
110
111
/**
112
* Programmatic SSL configuration
113
*/
114
@Bean
115
public ServletWebServerFactory servletContainer() {
116
TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();
117
tomcat.addAdditionalTomcatConnectors(createSSLConnector());
118
return tomcat;
119
}
120
121
private Connector createSSLConnector() {
122
Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
123
Http11NioProtocol protocol = (Http11NioProtocol) connector.getProtocolHandler();
124
125
connector.setScheme("https");
126
connector.setSecure(true);
127
connector.setPort(8443);
128
129
protocol.setSSLEnabled(true);
130
protocol.setKeystoreFile("classpath:keystore.p12");
131
protocol.setKeystorePass("changeit");
132
protocol.setKeystoreType("PKCS12");
133
protocol.setKeyAlias("tomcat");
134
135
return connector;
136
}
137
```
138
139
### Thread Pool Configuration
140
141
Configure server thread pools and connection handling.
142
143
```properties { .api }
144
# Tomcat thread configuration
145
server.tomcat.threads.max=200
146
server.tomcat.threads.min-spare=10
147
server.tomcat.accept-count=100
148
server.tomcat.max-connections=8192
149
```
150
151
```java { .api }
152
/**
153
* Custom thread pool configuration
154
*/
155
@Bean
156
public WebServerFactoryCustomizer<TomcatServletWebServerFactory> threadPoolCustomizer() {
157
return factory -> {
158
factory.addConnectorCustomizers(connector -> {
159
Http11NioProtocol protocol = (Http11NioProtocol) connector.getProtocolHandler();
160
protocol.setMaxThreads(300);
161
protocol.setMinSpareThreads(20);
162
protocol.setAcceptCount(150);
163
protocol.setMaxConnections(10000);
164
protocol.setConnectionTimeout(30000);
165
});
166
};
167
}
168
```
169
170
### Access Logging
171
172
Configure server access logging.
173
174
```properties { .api }
175
# Tomcat access log configuration
176
server.tomcat.accesslog.enabled=true
177
server.tomcat.accesslog.directory=logs
178
server.tomcat.accesslog.file-date-format=.yyyy-MM-dd
179
server.tomcat.accesslog.pattern=common
180
server.tomcat.accesslog.prefix=access_log
181
server.tomcat.accesslog.suffix=.log
182
server.tomcat.accesslog.rotate=true
183
```
184
185
```java { .api }
186
/**
187
* Programmatic access log configuration
188
*/
189
@Bean
190
public WebServerFactoryCustomizer<TomcatServletWebServerFactory> accessLogCustomizer() {
191
return factory -> {
192
factory.addEngineValves(new AccessLogValve() {{
193
setEnabled(true);
194
setDirectory("logs");
195
setPrefix("access_log");
196
setSuffix(".log");
197
setPattern("common");
198
setFileDateFormat(".yyyy-MM-dd");
199
}});
200
};
201
}
202
```
203
204
### Multiple Connectors
205
206
Configure multiple connectors for different protocols or ports.
207
208
```java { .api }
209
/**
210
* HTTP and HTTPS connectors
211
*/
212
@Bean
213
public ServletWebServerFactory servletContainer() {
214
TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();
215
216
// Add HTTP connector
217
tomcat.addAdditionalTomcatConnectors(createHttpConnector());
218
219
// Add HTTPS connector
220
tomcat.addAdditionalTomcatConnectors(createHttpsConnector());
221
222
return tomcat;
223
}
224
225
private Connector createHttpConnector() {
226
Connector connector = new Connector(TomcatServletWebServerFactory.DEFAULT_PROTOCOL);
227
connector.setPort(8080);
228
connector.setScheme("http");
229
connector.setSecure(false);
230
return connector;
231
}
232
233
private Connector createHttpsConnector() {
234
Connector connector = new Connector(TomcatServletWebServerFactory.DEFAULT_PROTOCOL);
235
connector.setPort(8443);
236
connector.setScheme("https");
237
connector.setSecure(true);
238
239
Http11NioProtocol protocol = (Http11NioProtocol) connector.getProtocolHandler();
240
protocol.setSSLEnabled(true);
241
// SSL configuration...
242
243
return connector;
244
}
245
```
246
247
### Graceful Shutdown
248
249
Configure graceful server shutdown behavior.
250
251
```properties { .api }
252
# Graceful shutdown configuration
253
server.shutdown=GRACEFUL
254
spring.lifecycle.timeout-per-shutdown-phase=30s
255
```
256
257
```java { .api }
258
/**
259
* Graceful shutdown customization
260
*/
261
@Bean
262
public GracefulShutdown gracefulShutdown() {
263
return new GracefulShutdown();
264
}
265
266
@Component
267
public class GracefulShutdownHandler {
268
269
@EventListener
270
public void handleShutdown(ContextClosedEvent event) {
271
// Custom shutdown logic
272
log.info("Application is shutting down gracefully...");
273
}
274
}
275
```
276
277
## Server Factory Types
278
279
```java { .api }
280
// Base servlet web server factory
281
public interface ServletWebServerFactory {
282
WebServer getWebServer(ServletContextInitializer... initializers);
283
}
284
285
// Tomcat server factory
286
public class TomcatServletWebServerFactory implements ServletWebServerFactory {
287
public void setPort(int port);
288
public void setContextPath(String contextPath);
289
public void setDisplayName(String displayName);
290
public void addConnectorCustomizers(TomcatConnectorCustomizer... customizers);
291
public void addAdditionalTomcatConnectors(Connector... connectors);
292
public void addEngineValves(Valve... valves);
293
}
294
295
// Jetty server factory
296
public class JettyServletWebServerFactory implements ServletWebServerFactory {
297
public void setPort(int port);
298
public void setContextPath(String contextPath);
299
public void addServerCustomizers(JettyServerCustomizer... customizers);
300
}
301
302
// Undertow server factory
303
public class UndertowServletWebServerFactory implements ServletWebServerFactory {
304
public void setPort(int port);
305
public void setContextPath(String contextPath);
306
public void addBuilderCustomizers(UndertowBuilderCustomizer... customizers);
307
public void addDeploymentInfoCustomizers(UndertowDeploymentInfoCustomizer... customizers);
308
}
309
310
// Web server factory customizer interface
311
@FunctionalInterface
312
public interface WebServerFactoryCustomizer<T extends WebServerFactory> {
313
void customize(T factory);
314
}
315
```