Sub topic 5
The embedded web server
This chapter assumes you know basic Java, can read a Maven build file, and understand HTTP requests and responses. It also assumes you have met starters, which the previous chapter covered.
The problem it solves
In traditional Java web development, you build a WAR file (a web archive). You then install a separate server such as Tomcat, configure it, and deploy the WAR into it. The server and the application have separate lifecycles, and the server's version and settings can differ between machines.
Spring Boot reverses this. The server is a library inside your application. When your main method runs, the application starts the server in the same process. The result is one artifact, a JAR file, that you run with java -jar.
Where the server comes from
You do not add the server by hand. The spring-boot-starter-web starter brings in Spring MVC, JSON support, and Tomcat as the default embedded server.
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>The Spring Boot parent manages the versions, so neither dependency declares one. Because Tomcat is on the classpath, auto-configuration detects it and creates the beans that start it. You write no server code.
What happens at startup
Startup logs confirm this. Look for lines such as Tomcat initialized with port 8080 and Tomcat started on port 8080.
Changing the port
The default port is 8080. You can change it in application.properties:
server.port=8081
spring.application.name=demoSetting server.port=0 asks the operating system for a free random port. This is useful in tests, where several applications may run at the same time.
Choosing a different server
Tomcat is the default, but Boot also supports Jetty and Undertow. To switch, exclude the Tomcat starter from the web starter and add the one you want.
<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>Auto-configuration then finds Jetty and starts it instead. Your controllers do not change, because they depend on Spring MVC and not on a specific server.
Common errors
- Port already in use. The log shows
Web server failed to start. Port 8080 was already in use.Stop the other process, or set a differentserver.port. - The application starts and exits immediately. No web starter is on the classpath, so no server is created. Check that
spring-boot-starter-webis in the build file. - Server settings ignored. The property name is probably misspelled. Boot does not warn about unknown properties in
application.properties, so check the spelling against theserver.*properties. - Expecting a WAR deployment to behave the same. If you deploy to an external server, the embedded one is not used, and
server.porthas no effect.
Trade-offs
Embedding makes deployment simple. Each service carries its own server version, and a container image needs only a JRE and the JAR. The cost is that every application bundles server code, so JARs are larger. Server upgrades also require rebuilding and redeploying the application, instead of patching a shared server once.
Exercise
You add spring-boot-starter-web to a new project, and the application starts on port 8080. You then set server.port=0 and restart. What changes, and why might you want this setting?
Show answer
The operating system assigns a random free port, and the log shows which one was chosen. This is useful in tests or when many instances run on one machine, because it avoids port conflicts.
Exercise
A teammate wants to run the application on Jetty instead of Tomcat. Which two changes are needed in the build file, and does the controller code change?
Show answer
Exclude spring-boot-starter-tomcat from spring-boot-starter-web, then add spring-boot-starter-jetty. The controller code does not change, because it depends on Spring MVC and not on a specific server.