What is Embedded Server Support in Spring Boot?
Spring Boot enhances Java web development by offering embedded server support, allowing applications to run independently without needing an external application server (like Tomcat or Jetty installed separately). This simplifies development, testing, and deployment, making Spring Boot ideal for microservices and cloud-native applications.
🔧 1. Embedded Server Options in Spring Boot
Spring Boot supports multiple embedded servlet containers:
- Apache Tomcat (default): High-performance, widely-used.
- Jetty: Lightweight and scalable.
- Undertow: Fast, non-blocking, and flexible.
✅ To switch servers:
Use Maven or Gradle to exclude Tomcat and include your preferred server:
Example: Use Jetty
xml
<!-- Exclude default Tomcat -->
<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>
<!-- Include Jetty -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
⚙️ 2. Embedded Server Configuration
You can configure the embedded server using application.properties
or application.yml
.
Common Properties:
properties
# Set port
server.port=8081
# Set context path
server.servlet.context-path=/myapp
# Bind to IP
server.address=0.0.0.0
# Session timeout
server.servlet.session.timeout=15m
🛠️ 3. Customizing Embedded Servers
Customize embedded server behavior using Java configuration classes.
a) Custom Tomcat Configuration
java
@Configuration
public class TomcatConfig {
@Bean
public WebServerFactoryCustomizer<TomcatServletWebServerFactory> customizer() {
return factory -> factory.addContextCustomizers(context -> {
// Customize Tomcat context
});
}
}
b) Custom Jetty Configuration
java
@Configuration
public class JettyConfig {
@Bean
public WebServerFactoryCustomizer<JettyServletWebServerFactory> customizer() {
return factory -> factory.addServerCustomizers(server -> {
// Customize Jetty server
});
}
}
c) Custom Undertow Configuration
java
@Configuration
public class UndertowConfig {
@Bean
public WebServerFactoryCustomizer<UndertowServletWebServerFactory> customizer() {
return factory -> factory.addBuilderCustomizers(builder -> {
// Customize Undertow builder
});
}
}
📦 4. Packaging and Running Applications
Spring Boot applications can be packaged as executable JARs or WARs.
✅ Run as a standalone JAR:
bash
mvn clean package
java -jar target/myapp.jar
✅ WAR Deployment:
For traditional WAR packaging, you can deploy to external servers or still run as a standalone if the embedded server is included.
🌟 5. Benefits of Embedded Server in Spring Boot
FeatureBenefitStand-alone ExecutionNo need for separate server installationSimplified DeploymentPackage once, run anywhere with java -jar
Reduced ConfigurationAuto-configured ports, contexts, and threadsFaster DevelopmentInstant startup, better for rapid prototyping and microservice design
🚫 6. Disabling the Embedded Server
In cases like non-web apps or traditional WAR deployments, you might want to disable the embedded server.
Option A: Disable via properties
properties
spring.main.web-application-type=none
Option B: Use provided
scope for web starter
xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<scope>provided</scope>
</dependency>
📝 Summary
Spring Boot’s embedded server support is a game changer for modern Java development. It lets you:
- Skip external servers
- Simplify deployment
- Configure with ease
- Launch apps with a single command
Whether you're building microservices, REST APIs, or full-stack applications, embedded server support in Spring Boot provides the speed, simplicity, and flexibility today's developers need.