Embedded servers are the superheroes that let you run your Spring Boot apps out of the box. You don't have to manually set up, install, or configure classic application servers like Apache Tomcat or Jetty anymore. Spring Boot handles that boring stuff for you, providing embedded servers.
Benefits of using embedded servers:
- Faster development and testing: starting the app takes just a couple of seconds. You just hit "Run".
- Simplified app delivery: the server is already bundled inside your JAR. You just deploy one file instead of dealing with server config.
- Better DevOps support: containerization (for example, Docker) gets easier because the whole app stack is in one place.
- Consistency between dev and prod: you can be confident the app will run the same everywhere — your machine, staging, or production.
So, if you used to spend hours setting up Tomcat, Spring Boot now basically tells you, "relax, I've got this."
1. Which servers does Spring Boot support?
Spring Boot supports several popular embedded servers. Here are the main ones:
| Server | Description |
|---|---|
| Tomcat | Used by default. It's so popular even cat memes take a break |
| Jetty | A lightweight server for developers who love minimalism |
| Undertow | A high-performance server that can handle millions of requests |
By default Apache Tomcat is used, but you can easily swap it out for Jetty or Undertow if you want.
2. Configuring the embedded server
2.1. Changing the server port
By default Spring Boot runs your server on port 8080. But what if 8080 is already taken? (For example, your favorite pet project about cats might be running on that port.)
To change the port, just set it in application.properties:
server.port=9090
Or, if you use YAML (because it looks more professional):
server:
port: 9090
Now your app will hear your HTTP requests on port 9090.
2.2. Changing the server
If Tomcat isn't your thing, you can hook up another server, like Jetty. It's super simple:
- Remove the
spring-boot-starter-tomcatdependency frompom.xml. - Add the
spring-boot-starter-jettydependency.
For Maven:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
For Gradle:
implementation 'org.springframework.boot:spring-boot-starter-jetty'
After restarting the project, Spring Boot will automatically switch to Jetty. Yep, it's that fast! Feels like you just changed the channel on the TV.
2.3. Tuning other settings
Spring Boot also lets you tweak lots of other server parameters. Here are some common ones:
- Maximum request body size:
server.tomcat.max-http-post-size=2097152 # 2MB - Connection timeout:
server.connection-timeout=5000 # 5000ms - Context path (if your app is available at
/myapp):server.servlet.context-path=/myapp
Now your app will be available at http://localhost:8080/myapp.
3. Embedded servers and microservices — a perfect match
In a microservice architecture each service lives its own life. Like a separate apartment in a big building — with its own kitchen, bathroom, and separate entrance. Embedded Spring Boot servers are great for this approach: they make each service fully autonomous.
What does that buy you in practice?
- Wanna update one service? Go for it! The others won't even notice
- Need to package a service into Docker? Easy — the server's already inside
- Forget about configuring one huge shared application server
Result? Compact, independent services you can deploy anywhere, anytime.
4. Embedded servers simplify CI/CD
When you start setting up automated builds and deployments (CI/CD), embedded servers will become your best friends. Why?
Imagine a pipeline:
- You build the app into a single JAR — like a box with everything inside
- You take that box and put it where it needs to go — it just runs
- No more "it doesn't start on the test server because the version is different"
Simple: where the JAR is, the server is, so the app will run the same everywhere.
5. Are embedded servers easy to use?
Absolutely! Let's write a small example.
Step 1: Create an app using embedded Tomcat
Using Spring Initializr (or manually in pom.xml), add the dependency:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
This starter will automatically pull in Tomcat.
Step 2: Write a simple controller
Create the file HelloController.java:
package com.example.demo;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@GetMapping("/")
public String sayHello() {
return "Hello from embedded Tomcat!";
}
}
Step 3: Run the app
Start the app. Now it's available at: http://localhost:8080. Tomcat is already ready to serve your requests.
6. Pitfalls (yes, they exist)
While embedded servers make life easier, there are a couple of caveats:
- App size: bundling an embedded server increases the JAR size. But that's the trade-off for convenience.
- Performance: each microservice runs its own server, which can demand more resources on VMs.
But if your apps are minimal and properly tuned, these issues are easy to handle.
So, embedded servers aren't just a tool — they're your go-to helpers for building fast, convenient, and portable apps. In the next lecture we'll practice building Spring Boot apps.
GO TO FULL VERSION