Starter for using Jetty as the embedded servlet container in Spring Boot applications
npx @tessl/cli install tessl/maven-org-springframework-boot--spring-boot-starter-jetty@3.5.0Spring Boot Starter Jetty provides Eclipse Jetty as an embedded servlet container for Spring Boot applications. It serves as a drop-in replacement for the default Tomcat container, offering advanced WebSocket support, custom server configurations, and specific performance characteristics that Jetty provides over other embedded containers.
spring-boot-starter-web with spring-boot-starter-jetty<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>For Gradle:
implementation 'org.springframework.boot:spring-boot-starter-jetty'import org.springframework.boot.web.embedded.jetty.JettyServletWebServerFactory;
import org.springframework.boot.web.embedded.jetty.JettyReactiveWebServerFactory;
import org.springframework.boot.web.embedded.jetty.JettyServerCustomizer;
import org.springframework.boot.web.embedded.jetty.ConfigurableJettyWebServerFactory;// In your main application or configuration class
@SpringBootApplication
public class JettyApplication {
public static void main(String[] args) {
SpringApplication.run(JettyApplication.class, args);
}
}<!-- Exclude Tomcat and include Jetty -->
<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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>@Configuration
public class JettyConfig {
@Bean
public JettyServletWebServerFactory jettyWebServerFactory() {
JettyServletWebServerFactory factory = new JettyServletWebServerFactory();
factory.setPort(8080);
factory.addServerCustomizers(new JettyServerCustomizer() {
@Override
public void customize(Server server) {
// Custom server configuration
}
});
return factory;
}
}Spring Boot Starter Jetty integrates with Spring Boot's auto-configuration system through several key components:
Core servlet container functionality using Jetty as the embedded server. Provides complete servlet container capabilities with Spring Boot integration.
public class JettyServletWebServerFactory
extends AbstractServletWebServerFactory
implements ConfigurableJettyWebServerFactory, ResourceLoaderAware {
public JettyServletWebServerFactory();
public JettyServletWebServerFactory(int port);
public JettyServletWebServerFactory(String contextPath, int port);
public WebServer getWebServer(ServletContextInitializer... initializers);
public void addServerCustomizers(JettyServerCustomizer... customizers);
public void setMaxConnections(int maxConnections);
public void setResourceLoader(ResourceLoader resourceLoader);
public Collection<JettyServerCustomizer> getServerCustomizers();
}Reactive web server implementation using Jetty for WebFlux applications. Provides non-blocking, reactive HTTP server capabilities.
public class JettyReactiveWebServerFactory
extends AbstractReactiveWebServerFactory
implements ConfigurableJettyWebServerFactory {
public JettyReactiveWebServerFactory();
public JettyReactiveWebServerFactory(int port);
public WebServer getWebServer(HttpHandler httpHandler);
public void addServerCustomizers(JettyServerCustomizer... customizers);
public void setMaxConnections(int maxConnections);
public void setResourceFactory(JettyResourceFactory resourceFactory);
public Collection<JettyServerCustomizer> getServerCustomizers();
}Comprehensive server configuration and customization options including SSL, threading, connectors, and advanced Jetty features.
public interface JettyServerCustomizer {
void customize(Server server);
}
public interface ConfigurableJettyWebServerFactory extends ConfigurableWebServerFactory {
void addServerCustomizers(JettyServerCustomizer... customizers);
void setUseForwardHeaders(boolean useForwardHeaders);
void setAcceptors(int acceptors);
void setSelectors(int selectors);
void setMaxConnections(int maxConnections);
void setThreadPool(ThreadPool threadPool);
}WebSocket capabilities for both servlet and reactive applications with Jakarta WebSocket API support.
public class JettyWebSocketServletWebServerCustomizer implements WebServerFactoryCustomizer<JettyServletWebServerFactory> {
public void customize(JettyServletWebServerFactory factory);
}
public class JettyWebSocketReactiveWebServerCustomizer implements WebServerFactoryCustomizer<JettyReactiveWebServerFactory> {
public void customize(JettyReactiveWebServerFactory factory);
}HTTP client builders for creating Jetty-based HTTP clients in Spring Boot applications.
public class JettyClientHttpRequestFactoryBuilder
implements ClientHttpRequestFactoryBuilder<JettyClientHttpRequestFactory> {
public JettyClientHttpRequestFactoryBuilder();
public JettyClientHttpRequestFactoryBuilder httpClient(HttpClient httpClient);
public JettyClientHttpRequestFactoryBuilder connectTimeout(Duration connectTimeout);
public JettyClientHttpRequestFactoryBuilder readTimeout(Duration readTimeout);
public JettyClientHttpRequestFactory build();
}
public class JettyHttpClientBuilder {
public JettyHttpClientBuilder();
public JettyHttpClientBuilder sslContextFactory(SslContextFactory.Client sslContextFactory);
public JettyHttpClientBuilder connectTimeout(Duration connectTimeout);
public JettyHttpClientBuilder idleTimeout(Duration idleTimeout);
public HttpClient build();
}Spring Boot auto-configuration classes that automatically configure Jetty based on application properties and classpath detection.
public class JettyWebServerFactoryCustomizer
implements WebServerFactoryCustomizer<ConfigurableJettyWebServerFactory> {
public JettyWebServerFactoryCustomizer(ServerProperties serverProperties,
ObjectProvider<JettyThreadPool> threadPool);
public void customize(ConfigurableJettyWebServerFactory factory);
}
public class JettyVirtualThreadsWebServerFactoryCustomizer
implements WebServerFactoryCustomizer<ConfigurableJettyWebServerFactory> {
public JettyVirtualThreadsWebServerFactoryCustomizer();
public void customize(ConfigurableJettyWebServerFactory factory);
}
public class JettyThreadPool implements ThreadPool {
public static JettyThreadPool create(ServerProperties.Jetty jettyProperties);
public JettyThreadPool();
public JettyThreadPool(int maxThreads, int minThreads, int idleTimeout);
public void setMaxThreads(int maxThreads);
public void setMinThreads(int minThreads);
public void setIdleTimeout(int idleTimeout);
public void execute(Runnable job);
public boolean isLowOnThreads();
}public class JettyWebServer implements WebServer {
public JettyWebServer(Server server);
public JettyWebServer(Server server, boolean autoStart);
public void start() throws WebServerException;
public void stop() throws WebServerException;
public int getPort();
public Server getServer();
public void shutDownGracefully(GracefulShutdownCallback callback);
public void destroy();
}
public class JettyEmbeddedWebAppContext extends WebAppContext {
public JettyEmbeddedWebAppContext();
public JettyEmbeddedWebAppContext(String webApp, String contextPath);
public void setTempDirectory(File tempDirectory);
public void setParentLoaderPriority(boolean parentLoaderPriority);
public void setSessionTimeout(int sessionTimeout);
}
public class JettyEmbeddedErrorHandler extends ErrorHandler {
public JettyEmbeddedErrorHandler();
public void handle(String target, Request baseRequest,
HttpServletRequest request, HttpServletResponse response);
public void setShowServlet(boolean showServlet);
public void setShowStacks(boolean showStacks);
}
public class GracefulShutdown {
public GracefulShutdown();
public void shutDownGracefully(GracefulShutdownCallback callback);
public void setTimeout(Duration timeout);
}public class JettyResourceFactory implements DisposableBean, InitializingBean {
public JettyResourceFactory();
public HttpClient getHttpClient();
public Executor getExecutor();
public Scheduler getScheduler();
public ByteBufferPool getByteBufferPool();
public void setThreadPool(ThreadPool threadPool);
public void setExecutor(Executor executor);
public void setScheduler(Scheduler scheduler);
public void setByteBufferPool(ByteBufferPool byteBufferPool);
public void afterPropertiesSet() throws Exception;
public void destroy() throws Exception;
}public class SslServerCustomizer implements JettyServerCustomizer {
public SslServerCustomizer();
public void customize(Server server);
public void configureSslContextFactory(SslContextFactory.Server sslContextFactory);
public void setIncludeProtocols(String... protocols);
public void setIncludeCipherSuites(String... cipherSuites);
public void setNeedClientAuth(boolean needClientAuth);
public void setWantClientAuth(boolean wantClientAuth);
}
public class ForwardHeadersCustomizer implements JettyServerCustomizer {
public ForwardHeadersCustomizer();
public void customize(Server server);
public void setTrustedProxies(String... trustedProxies);
public void setForwardedHostHeader(String forwardedHostHeader);
public void setForwardedPortHeader(String forwardedPortHeader);
public void setForwardedProtoHeader(String forwardedProtoHeader);
}Jetty-specific configuration properties available through Spring Boot's server.jetty.* namespace:
server:
jetty:
acceptors: 2 # Number of acceptor threads
selectors: 4 # Number of selector threads
max-http-post-size: 10MB # Maximum HTTP POST size
threads:
max: 200 # Maximum thread pool size
min: 10 # Minimum thread pool size
idle-timeout: 60s # Thread idle timeout
connection:
idle-timeout: 30s # Connection idle timeout
accept-queue-size: 128 # Accept queue size