Core starter providing fundamental building blocks for Spring Boot applications with auto-configuration support, logging, and YAML parsing
—
Comprehensive web application support including embedded servers (Tomcat, Jetty, Undertow, Netty) for both servlet and reactive applications.
Core abstractions for web server management.
/**
* Simple interface that represents a fully configured web server
*/
public interface WebServer {
/**
* Starts the web server. Calling this method on an already started server has no effect
* @throws WebServerException if the server cannot be started
*/
void start() throws WebServerException;
/**
* Stops the web server. Calling this method on an already stopped server has no effect
* @throws WebServerException if the server cannot be stopped
*/
void stop() throws WebServerException;
/**
* Return the port this server is listening on
* @return the port (or -1 if none)
*/
int getPort();
/**
* Gracefully shutdown the web server
* @param callback the callback to invoke when the graceful shutdown completes
*/
default void shutDownGracefully(GracefulShutdownCallback callback);
}
/**
* Factory interface that can be used to create a WebServer
*/
@FunctionalInterface
public interface WebServerFactory {
/**
* Gets a new fully configured but paused WebServer instance
* @return a new WebServer instance
*/
WebServer getWebServer();
}
/**
* A configurable WebServerFactory
*/
public interface ConfigurableWebServerFactory extends WebServerFactory {
/**
* Set the port that the web server should listen on
* @param port the port to set
*/
void setPort(int port);
/**
* Set the specific network address that the server should bind to
* @param address the address to set
*/
void setAddress(InetAddress address);
/**
* Set the error pages that will be used when handling exceptions
* @param errorPages the error pages
*/
void setErrorPages(Set<? extends ErrorPage> errorPages);
/**
* Set the SSL configuration that will be applied to the server's default connector
* @param ssl the SSL configuration
*/
void setSsl(Ssl ssl);
/**
* Set the HTTP/2 configuration that will be applied to the server
* @param http2 the HTTP/2 configuration
*/
void setHttp2(Http2 http2);
/**
* Set the compression configuration that will be applied to the server's default connector
* @param compression the compression configuration
*/
void setCompression(Compression compression);
/**
* Set the server header value
* @param serverHeader the server header value
*/
void setServerHeader(String serverHeader);
/**
* Set the shutdown configuration that will be applied to the server
* @param shutdown the shutdown configuration
*/
void setShutdown(Shutdown shutdown);
}Servlet-based web server factories and configurations.
/**
* Factory interface that can be used to create servlet-based WebServer instances
*/
@FunctionalInterface
public interface ServletWebServerFactory extends WebServerFactory {
/**
* Gets a new fully configured but paused WebServer instance
* @param initializers servlet context initializers that should be applied
* @return a new WebServer instance
*/
WebServer getWebServer(ServletContextInitializer... initializers);
}
/**
* A configurable ServletWebServerFactory
*/
public interface ConfigurableServletWebServerFactory extends ServletWebServerFactory, ConfigurableWebServerFactory {
/**
* Set the context path for the web server
* @param contextPath the context path
*/
void setContextPath(String contextPath);
/**
* Set the display name of the application deployed in the web server
* @param displayName the display name
*/
void setDisplayName(String displayName);
/**
* Sets the configuration that will be applied to the container's HTTP session support
* @param session the session configuration
*/
void setSession(Session session);
/**
* Set the JSP servlet configuration that will be applied to the server
* @param jsp the JSP servlet configuration
*/
void setJsp(Jsp jsp);
/**
* Set the Locale to Charset mappings
* @param localeCharsetMappings the Locale to Charset mappings
*/
void setLocaleCharsetMappings(Map<Locale, Charset> localeCharsetMappings);
/**
* Set the init parameters that are applied to the server's ServletContext
* @param initParameters the init parameters
*/
void setInitParameters(Map<String, String> initParameters);
/**
* Add ServletContextInitializer instances that should be applied in addition to those that are configured by this factory
* @param initializers the initializers to add
*/
void addInitializers(ServletContextInitializer... initializers);
}
/**
* Base class for ConfigurableServletWebServerFactory implementations
*/
public abstract class AbstractServletWebServerFactory extends AbstractConfigurableWebServerFactory
implements ConfigurableServletWebServerFactory {
private String contextPath = "";
private String displayName;
private boolean registerDefaultServlet = false;
private Session session = new Session();
/**
* Create a new AbstractServletWebServerFactory instance
*/
public AbstractServletWebServerFactory();
/**
* Create a new AbstractServletWebServerFactory instance with the specified port
* @param port the port number for the embedded servlet server
*/
public AbstractServletWebServerFactory(int port);
/**
* Create a new AbstractServletWebServerFactory instance with the specified context path and port
* @param contextPath the context path for the embedded servlet server
* @param port the port number for the embedded servlet server
*/
public AbstractServletWebServerFactory(String contextPath, int port);
}Reactive web server factories for non-blocking applications.
/**
* Factory interface that can be used to create reactive WebServer instances
*/
@FunctionalInterface
public interface ReactiveWebServerFactory extends WebServerFactory {
/**
* Gets a new fully configured but paused WebServer instance
* @param httpHandler the HTTP handler in charge of processing requests
* @return a new WebServer instance
*/
WebServer getWebServer(HttpHandler httpHandler);
}
/**
* A configurable ReactiveWebServerFactory
*/
public interface ConfigurableReactiveWebServerFactory extends ReactiveWebServerFactory, ConfigurableWebServerFactory {
}
/**
* Base class for ConfigurableReactiveWebServerFactory implementations
*/
public abstract class AbstractReactiveWebServerFactory extends AbstractConfigurableWebServerFactory
implements ConfigurableReactiveWebServerFactory {
/**
* Create a new AbstractReactiveWebServerFactory instance
*/
public AbstractReactiveWebServerFactory();
/**
* Create a new AbstractReactiveWebServerFactory instance with the specified port
* @param port the port number for the embedded reactive server
*/
public AbstractReactiveWebServerFactory(int port);
}/**
* TomcatServletWebServerFactory that can be used to create TomcatWebServers
*/
public class TomcatServletWebServerFactory extends AbstractServletWebServerFactory
implements ConfigurableTomcatWebServerFactory, ResourceLoaderAware {
/**
* Create a new TomcatServletWebServerFactory instance
*/
public TomcatServletWebServerFactory();
/**
* Create a new TomcatServletWebServerFactory instance with the specified port
* @param port the port number for the new server instance
*/
public TomcatServletWebServerFactory(int port);
/**
* Create a new TomcatServletWebServerFactory instance with the specified context path and port
* @param contextPath the context path for the new server instance
* @param port the port number for the new server instance
*/
public TomcatServletWebServerFactory(String contextPath, int port);
@Override
public WebServer getWebServer(ServletContextInitializer... initializers);
/**
* Add TomcatContextCustomizer instances that should be applied to the Tomcat Context
* @param tomcatContextCustomizers the customizers to add
*/
public void addContextCustomizers(TomcatContextCustomizer... tomcatContextCustomizers);
/**
* Add TomcatConnectorCustomizer instances that should be applied to the Tomcat Connector
* @param tomcatConnectorCustomizers the customizers to add
*/
public void addConnectorCustomizers(TomcatConnectorCustomizer... tomcatConnectorCustomizers);
/**
* Add TomcatProtocolHandlerCustomizer instances that should be applied to the Tomcat protocol handlers
* @param tomcatProtocolHandlerCustomizers the customizers to add
*/
public void addProtocolHandlerCustomizers(TomcatProtocolHandlerCustomizer<?>... tomcatProtocolHandlerCustomizers);
}
/**
* TomcatReactiveWebServerFactory that can be used to create TomcatWebServers
*/
public class TomcatReactiveWebServerFactory extends AbstractReactiveWebServerFactory
implements ConfigurableTomcatWebServerFactory, ResourceLoaderAware {
/**
* Create a new TomcatReactiveWebServerFactory instance
*/
public TomcatReactiveWebServerFactory();
/**
* Create a new TomcatReactiveWebServerFactory instance with the specified port
* @param port the port number for the new server instance
*/
public TomcatReactiveWebServerFactory(int port);
@Override
public WebServer getWebServer(HttpHandler httpHandler);
}
/**
* WebServer that can be used to control a Tomcat web server
*/
public class TomcatWebServer implements WebServer {
/**
* Create a new TomcatWebServer instance
* @param tomcat the underlying Tomcat server
*/
public TomcatWebServer(Tomcat tomcat);
/**
* Create a new TomcatWebServer instance
* @param tomcat the underlying Tomcat server
* @param autoStart if the server should be started
*/
public TomcatWebServer(Tomcat tomcat, boolean autoStart);
@Override
public void start() throws WebServerException;
@Override
public void stop() throws WebServerException;
@Override
public int getPort();
}/**
* JettyServletWebServerFactory that can be used to create JettyWebServers
*/
public class JettyServletWebServerFactory extends AbstractServletWebServerFactory
implements ConfigurableJettyWebServerFactory {
/**
* Create a new JettyServletWebServerFactory instance
*/
public JettyServletWebServerFactory();
/**
* Create a new JettyServletWebServerFactory instance with the specified port
* @param port the port number for the new server instance
*/
public JettyServletWebServerFactory(int port);
/**
* Create a new JettyServletWebServerFactory instance with the specified context path and port
* @param contextPath the context path for the new server instance
* @param port the port number for the new server instance
*/
public JettyServletWebServerFactory(String contextPath, int port);
@Override
public WebServer getWebServer(ServletContextInitializer... initializers);
/**
* Add JettyServerCustomizer instances that should be applied to the Jetty Server
* @param customizers the customizers to add
*/
public void addServerCustomizers(JettyServerCustomizer... customizers);
}
/**
* JettyReactiveWebServerFactory that can be used to create JettyWebServers
*/
public class JettyReactiveWebServerFactory extends AbstractReactiveWebServerFactory
implements ConfigurableJettyWebServerFactory {
/**
* Create a new JettyReactiveWebServerFactory instance
*/
public JettyReactiveWebServerFactory();
/**
* Create a new JettyReactiveWebServerFactory instance with the specified port
* @param port the port number for the new server instance
*/
public JettyReactiveWebServerFactory(int port);
@Override
public WebServer getWebServer(HttpHandler httpHandler);
}
/**
* WebServer that can be used to control a Jetty web server
*/
public class JettyWebServer implements WebServer {
/**
* Create a new JettyWebServer instance
* @param server the underlying Jetty server
*/
public JettyWebServer(Server server);
/**
* Create a new JettyWebServer instance
* @param server the underlying Jetty server
* @param autoStart if the server should be started
*/
public JettyWebServer(Server server, boolean autoStart);
@Override
public void start() throws WebServerException;
@Override
public void stop() throws WebServerException;
@Override
public int getPort();
}/**
* UndertowServletWebServerFactory that can be used to create UndertowWebServers
*/
public class UndertowServletWebServerFactory extends AbstractServletWebServerFactory
implements ConfigurableUndertowWebServerFactory {
/**
* Create a new UndertowServletWebServerFactory instance
*/
public UndertowServletWebServerFactory();
/**
* Create a new UndertowServletWebServerFactory instance with the specified port
* @param port the port number for the new server instance
*/
public UndertowServletWebServerFactory(int port);
/**
* Create a new UndertowServletWebServerFactory instance with the specified context path and port
* @param contextPath the context path for the new server instance
* @param port the port number for the new server instance
*/
public UndertowServletWebServerFactory(String contextPath, int port);
@Override
public WebServer getWebServer(ServletContextInitializer... initializers);
/**
* Add UndertowBuilderCustomizer instances that should be applied to the Undertow Builder
* @param customizers the customizers to add
*/
public void addBuilderCustomizers(UndertowBuilderCustomizer... customizers);
/**
* Add UndertowDeploymentInfoCustomizer instances that should be applied to the Undertow DeploymentInfo
* @param customizers the customizers to add
*/
public void addDeploymentInfoCustomizers(UndertowDeploymentInfoCustomizer... customizers);
}
/**
* UndertowReactiveWebServerFactory that can be used to create UndertowWebServers
*/
public class UndertowReactiveWebServerFactory extends AbstractReactiveWebServerFactory
implements ConfigurableUndertowWebServerFactory {
/**
* Create a new UndertowReactiveWebServerFactory instance
*/
public UndertowReactiveWebServerFactory();
/**
* Create a new UndertowReactiveWebServerFactory instance with the specified port
* @param port the port number for the new server instance
*/
public UndertowReactiveWebServerFactory(int port);
@Override
public WebServer getWebServer(HttpHandler httpHandler);
}
/**
* WebServer that can be used to control an Undertow web server
*/
public class UndertowWebServer implements WebServer {
/**
* Create a new UndertowWebServer instance
* @param builder the undertow builder
* @param autoStart if the server should be started
*/
public UndertowWebServer(Builder builder, boolean autoStart);
@Override
public void start() throws WebServerException;
@Override
public void stop() throws WebServerException;
@Override
public int getPort();
}/**
* NettyReactiveWebServerFactory that can be used to create NettyWebServers
*/
public class NettyReactiveWebServerFactory extends AbstractReactiveWebServerFactory
implements ConfigurableNettyWebServerFactory {
/**
* Create a new NettyReactiveWebServerFactory instance
*/
public NettyReactiveWebServerFactory();
/**
* Create a new NettyReactiveWebServerFactory instance with the specified port
* @param port the port number for the new server instance
*/
public NettyReactiveWebServerFactory(int port);
@Override
public WebServer getWebServer(HttpHandler httpHandler);
/**
* Add NettyServerCustomizer instances that should be applied to the Netty server
* @param customizers the customizers to add
*/
public void addServerCustomizers(NettyServerCustomizer... customizers);
}
/**
* WebServer that can be used to control a Netty web server
*/
public class NettyWebServer implements WebServer {
/**
* Create a new NettyWebServer instance
* @param httpServer the underlying Netty HTTP server
* @param lifecycleTimeout the lifecycle timeout
*/
public NettyWebServer(HttpServer httpServer, Duration lifecycleTimeout);
@Override
public void start() throws WebServerException;
@Override
public void stop() throws WebServerException;
@Override
public int getPort();
}Builders and customizers for creating configured HTTP clients.
/**
* Builder that can be used to configure and create a RestTemplate
*/
public class RestTemplateBuilder {
/**
* Create a new RestTemplateBuilder instance
*/
public RestTemplateBuilder();
/**
* Set the read timeout for the underlying URLConnection
* @param readTimeout the read timeout
* @return a new builder instance
*/
public RestTemplateBuilder setReadTimeout(Duration readTimeout);
/**
* Set the connection timeout for the underlying URLConnection
* @param connectTimeout the connection timeout
* @return a new builder instance
*/
public RestTemplateBuilder setConnectTimeout(Duration connectTimeout);
/**
* Set the root URI that should be applied to each request
* @param rootUri the root URI
* @return a new builder instance
*/
public RestTemplateBuilder rootUri(String rootUri);
/**
* Add HTTP message converters that should be applied to the RestTemplate
* @param messageConverters the message converters to add
* @return a new builder instance
*/
public RestTemplateBuilder messageConverters(HttpMessageConverter<?>... messageConverters);
/**
* Add RestTemplateCustomizers that should be applied to the RestTemplate
* @param customizers the customizers to add
* @return a new builder instance
*/
public RestTemplateBuilder customizers(RestTemplateCustomizer... customizers);
/**
* Add HTTP client interceptors that should be applied to the RestTemplate
* @param interceptors the interceptors to add
* @return a new builder instance
*/
public RestTemplateBuilder interceptors(ClientHttpRequestInterceptor... interceptors);
/**
* Build a new RestTemplate instance and configure it using this builder
* @return a configured RestTemplate instance
*/
public RestTemplate build();
/**
* Build a new RestTemplate instance based on the provided class and configure it using this builder
* @param restTemplateClass the RestTemplate class to instantiate
* @return a configured RestTemplate instance
*/
public <T extends RestTemplate> T build(Class<T> restTemplateClass);
}
/**
* Callback interface that can be used to customize a RestTemplate
*/
@FunctionalInterface
public interface RestTemplateCustomizer {
/**
* Callback to customize a RestTemplate instance
* @param restTemplate the template to customize
*/
void customize(RestTemplate restTemplate);
}
/**
* Callback interface that can be used to customize a REST client
*/
@FunctionalInterface
public interface RestClientCustomizer {
/**
* Callback to customize a RestClient.Builder instance
* @param restClientBuilder the builder to customize
*/
void customize(RestClient.Builder restClientBuilder);
}Usage Examples:
// Basic RestTemplate configuration
@Configuration
public class WebConfig {
@Bean
public RestTemplate restTemplate(RestTemplateBuilder builder) {
return builder
.setConnectTimeout(Duration.ofSeconds(10))
.setReadTimeout(Duration.ofSeconds(30))
.rootUri("https://api.example.com")
.build();
}
}
// Custom embedded server configuration
@Configuration
public class ServerConfig {
@Bean
public WebServerFactoryCustomizer<TomcatServletWebServerFactory> tomcatCustomizer() {
return factory -> {
factory.setPort(9090);
factory.setContextPath("/api");
factory.addConnectorCustomizers(connector -> {
connector.setMaxThreads(200);
connector.setMinSpareThreads(10);
});
};
}
}Install with Tessl CLI
npx tessl i tessl/maven-org-springframework-boot--spring-boot-starter