📖 Scenario: You are building a simple Spring Boot web application that runs with an embedded server. This means the server is included inside your application, so you don't need to install or configure a separate web server.This setup is common for microservices and makes deployment easier.
🎯 Goal: Create a basic Spring Boot application with an embedded Tomcat server that starts automatically and serves a simple web page.
📋 What You'll Learn
Create a Spring Boot application class with the @SpringBootApplication annotation
Add a configuration property to set the server port to 8081
Create a REST controller with a GET endpoint /hello that returns a welcome message
Ensure the application runs with the embedded server on port 8081
💡 Why This Matters
🌍 Real World
Embedded servers allow developers to package web applications with their own server, simplifying deployment and making applications portable.
💼 Career
Understanding embedded servers is essential for modern Java backend development, especially in microservices and cloud-native applications.
Progress0 / 4 steps
1
Create the Spring Boot application class
Create a class called EmbeddedServerApplication in package com.example.demo. Annotate it with @SpringBootApplication and add the main method that runs SpringApplication.run(EmbeddedServerApplication.class, args).
Spring Boot
Hint
Use @SpringBootApplication on the class and add a main method that calls SpringApplication.run.
2
Configure the embedded server port
Create a file named application.properties in src/main/resources. Add the line server.port=8081 to set the embedded server port to 8081.
Spring Boot
Hint
The embedded server port is set in application.properties with server.port=8081.
3
Create a REST controller with a GET endpoint
Create a class called HelloController in package com.example.demo. Annotate it with @RestController. Add a method sayHello annotated with @GetMapping("/hello") that returns the string "Welcome to the embedded server!".
Spring Boot
Hint
Use @RestController on the class and @GetMapping("/hello") on the method that returns the welcome string.
4
Run the application with embedded server
Ensure the application class EmbeddedServerApplication has the main method that runs the Spring Boot application. This will start the embedded Tomcat server on port 8081 automatically.
Spring Boot
Hint
The main method must call SpringApplication.run to start the embedded server.
Practice
(1/5)
1. What is the main advantage of using an embedded server in a Spring Boot application?
easy
A. It requires manual installation of a web server before running the app.
B. It allows the application to run standalone without needing an external server.
C. It disables HTTP support in the application.
D. It forces the use of XML configuration files.
Solution
Step 1: Understand embedded server purpose
Embedded servers are included inside the app, so no external server setup is needed.
Step 2: Compare options
Only It allows the application to run standalone without needing an external server. correctly states the standalone running advantage. Others describe incorrect or unrelated facts.
Final Answer:
It allows the application to run standalone without needing an external server. -> Option B
Quick Check:
Embedded server = standalone app [OK]
Hint: Embedded server means no external server needed [OK]
Common Mistakes:
Thinking external server is still required
Confusing embedded server with disabling HTTP
Assuming XML config is mandatory
2. Which dependency is commonly used to include an embedded Tomcat server in a Spring Boot project?
easy
A. spring-boot-starter-security
B. spring-boot-starter-data-jpa
C. spring-boot-starter-test
D. spring-boot-starter-web
Solution
Step 1: Identify embedded server dependency
The embedded Tomcat server comes with the spring-boot-starter-web dependency.
Step 2: Eliminate unrelated dependencies
Other options relate to data, security, or testing, not embedded servers.
Final Answer:
spring-boot-starter-web -> Option D
Quick Check:
Web starter includes embedded Tomcat [OK]
Hint: Web starter includes embedded Tomcat by default [OK]
Common Mistakes:
Choosing data or security starters for embedded server
Confusing test starter with server dependency
3. Given this Spring Boot application.properties setting: server.port=8085 What will happen when you run the app with embedded Tomcat?
medium
A. The embedded server will start on port 8085.
B. The server will ignore this and use default port 8080.
C. The app will fail to start due to invalid port.
D. The server will start on port 80.
Solution
Step 1: Understand server.port property
Setting server.port changes the embedded server's listening port.
Step 2: Apply property effect
Since 8085 is valid, the embedded Tomcat will start on port 8085.
Final Answer:
The embedded server will start on port 8085. -> Option A
Quick Check:
server.port sets embedded server port [OK]
Hint: server.port config changes embedded server port [OK]
Common Mistakes:
Assuming default port always used
Thinking invalid port causes failure (8085 is valid)
Confusing port 80 as default
4. Consider this Spring Boot main class snippet:
public static void main(String[] args) {
SpringApplication.run(MyApp.class, args);
}
If you remove the embedded server dependency, what will happen when you run this app?
medium
A. The app will start with an external server automatically.
B. The app will fail to start with a missing server error.
C. The app will start but no web server will run.
D. The app will start and embed Jetty server by default.
Solution
Step 1: Understand dependency role
spring-boot-starter-web provides the embedded server. Without it, Spring Boot runs as a non-web application.
Step 2: Effect of removing embedded server dependency
The app starts successfully, but no web server runs since no web dependencies are present.
Final Answer:
The app will start but no web server will run. -> Option C
Quick Check:
No web starter = app starts, no server [OK]
Hint: No web starter = app starts without server [OK]
Common Mistakes:
Thinking app fails to start without server dependency
Believing external server auto-starts
Assuming Jetty starts by default without dependency
5. You want to switch your Spring Boot app's embedded server from Tomcat to Jetty. Which change is required?
hard
A. Exclude spring-boot-starter-tomcat and add spring-boot-starter-jetty dependency.
B. Add spring-boot-starter-jetty without excluding Tomcat.
C. Change server.port to 9090 in application.properties.
D. Add spring-boot-starter-undertow dependency only.
Solution
Step 1: Understand default embedded server
Spring Boot uses Tomcat by default with spring-boot-starter-web.
Step 2: How to switch embedded server
You must exclude Tomcat dependency and add Jetty starter to replace it.
Final Answer:
Exclude spring-boot-starter-tomcat and add spring-boot-starter-jetty dependency. -> Option A
Quick Check:
Exclude Tomcat + add Jetty = switch server [OK]
Hint: Exclude Tomcat before adding Jetty starter [OK]
Common Mistakes:
Adding Jetty without excluding Tomcat causes conflicts
Changing port does not switch server
Adding Undertow starter switches to Undertow, not Jetty
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.