Lesson 2 of 6
Intro to Spring Boot
Estimated time: 2–2.5 hours
What You Will Learn
- Understand what Spring Boot is and why it is popular
- Create a new Spring Boot project using start.spring.io
- Understand the project folder structure
- Build and run your first REST endpoint
- Understand the full request/response flow
Part 1 — What is Spring Boot?
In the previous lesson, you installed Java and got comfortable running simple Java programs from the command line. That was an important first step. But if you have ever wondered how real companies like Netflix, Amazon, or your bank build the software that powers their websites and mobile apps, the answer often involves something called Spring Boot. In this lesson, you are going to learn what Spring Boot is, why developers love it, and — most importantly — you are going to build something with it yourself.
Before we talk about Spring Boot, let us quickly talk about the Spring Framework. Spring is a massive collection of tools and libraries for building Java applications. It was created back in 2003 to make Java development easier. Think of the Spring Framework as an enormous toolbox — it has a tool for virtually every job you could imagine: connecting to databases, sending emails, handling security, building web pages, and much more. The problem? That toolbox is huge. Setting up a Spring project from scratch used to require dozens of configuration files, and you had to manually tell Spring exactly how every piece should fit together. For beginners, it felt overwhelming.
That is where Spring Boot comes in. Spring Boot was released in 2014 to solve exactly this problem. It sits on top of the Spring Framework and handles all the tedious configuration for you automatically. Instead of spending hours writing setup files, you can have a working project in minutes. Spring Boot does not replace Spring — it makes Spring dramatically easier to use.
Here is an analogy that might help. Imagine you want to build a kitchen. Plain Java is like having access to raw materials — lumber, nails, pipes, and wire. You can build a kitchen, but you need to know carpentry, plumbing, and electrical work. The Spring Framework is like getting pre-cut lumber and labeled pipes — it helps, but you still need to figure out how everything connects. Spring Boot is like getting a prefab kitchen kit — the cabinets come pre-assembled, the pipes are pre-fitted, and the instructions say "just bolt it together." You still need to know what a kitchen is and how to use it, but the hard assembly work is done for you.
So why is Spring Boot so popular? Three big reasons:
Why Developers Love Spring Boot
- Convention over configuration. Spring Boot makes sensible default choices for you. Instead of configuring every detail yourself, you only need to change the settings where you disagree with the defaults. This saves an enormous amount of time.
- Embedded web server. Traditional Java web applications required you to install and configure a separate web server (like Apache Tomcat) on your computer. Spring Boot includes Tomcat built right into your project. When you run your application, the web server starts automatically. No extra installation needed.
- Starter dependencies. Need to build a web API? Just add the "Spring Web" starter, and Spring Boot pulls in everything you need. Need a database? Add the "Spring Data JPA" starter. These starters are pre-packaged bundles of libraries that work together, so you never have to hunt down individual pieces.
What kinds of things can you build with Spring Boot? Quite a lot, actually. The most common use cases include REST APIs (the backend services that mobile apps and websites talk to), web applications (full websites with server-rendered pages), and microservices (small, focused services that work together to form a larger system). Many of the apps you use every day have Spring Boot running behind the scenes.
Here is the exciting part: remember the contact form you built in the earlier web basics lessons? That form had HTML fields for a name, email, and message, and you probably used JavaScript to handle the front end. But when someone clicks "Submit," where does that data actually go? Right now, it goes nowhere — there is no server to receive it. Over the next few lessons, you are going to build a real backend server with Spring Boot that can receive that form data, process it, and send back a response. By the end of this track, your contact form will actually work end to end.
But we are getting ahead of ourselves. Today, we start small. You are going to create your very first Spring Boot project, understand how it is organized, and build a simple endpoint that responds when you visit a URL in your browser. This is the foundation that everything else will be built on.
One more thing before we dive in: you do not need to be a Java expert to follow along. If you completed the previous lesson and can write basic Java code (variables, methods, classes), you have enough knowledge. Spring Boot handles much of the complexity, and we will explain every new concept as it comes up. Let us get started.
Part 2 — Creating Your First Spring Boot Project
One of the best things about Spring Boot is that the team behind it built an incredible tool for generating new projects. It is called the Spring Initializr, and it lives at start.spring.io. Think of it as a project generator — you tell it what kind of application you want to build, pick a few options, and it creates a ready-to-go project for you as a downloadable zip file. No manual setup required.
Open your web browser and navigate to start.spring.io. You will see a form with several options. Let us walk through each one carefully. Do not worry if some of these terms are unfamiliar — we will explain each one.
Project type: You will see two options — Maven and Gradle. Select Maven. Maven is a build tool for Java projects. It manages your project's dependencies (the external libraries your project uses) and handles compiling and running your code. Think of Maven as a project manager that keeps everything organized. We choose Maven because it is the most common choice for beginners and has excellent documentation.
Language: Select Java. Spring Boot also supports Kotlin and Groovy, but since we have been learning Java, we will stick with it.
Spring Boot version: Select the latest stable version. At the time of writing, this is 3.4.x. Avoid any version labeled "SNAPSHOT" or "M1" or "RC" — those are pre-release versions that might have bugs. The stable version (without any suffix) is the one you want.
Project Metadata: This section describes your project. Fill it in exactly like this:
| Field | Value | What It Means |
|---|---|---|
| Group | org.codersfarm |
Your organization's identifier, written in reverse domain name format. This helps prevent naming conflicts with other projects around the world. |
| Artifact | contact-api |
The name of your project. This becomes the name of the generated zip file and the project folder. |
| Name | contact-api |
A human-readable name for your project. Usually the same as the artifact. |
| Description | Contact form API for Coders Farm |
A short description of what this project does. |
| Package name | org.codersfarm.contactapi |
The base Java package for your code. This is automatically generated from the Group and Artifact fields. |
| Packaging | Jar |
The format of your final build output. Jar (Java Archive) is the standard for Spring Boot. |
| Java version | 21 |
The version of Java your project will use. Select 21, which is the latest Long-Term Support (LTS) release. |
Dependencies: This is where you tell Spring Boot what features your project needs. On the right side of the page, click the "Add Dependencies" button (or press Ctrl+B). A search box will appear. Type "Spring Web" and select it from the list. This single dependency gives your project everything it needs to build web applications and REST APIs, including the embedded Tomcat web server.
For now, that is the only dependency we need. In future lessons, we will add more (like database support), but for today Spring Web is all we need to get started.
Double-check that everything looks correct, then click the big "Generate" button at the bottom of the page. Your browser will download a file called contact-api.zip. Find that file in your Downloads folder and unzip it. You should now have a folder called contact-api.
Now open this project in your code editor. If you are using VS Code, you can open a terminal, navigate to the folder, and type code . to open the entire project. If you are using IntelliJ IDEA, choose "Open" from the welcome screen and select the contact-api folder. IntelliJ will automatically detect that it is a Maven project and begin importing the dependencies. This might take a minute or two the first time as it downloads everything.
Part 3 — Understanding the Project Structure
Now that you have your project open in your editor, let us take a tour of the files and folders that Spring Initializr generated for you. Understanding this structure is important because every Spring Boot project follows the same layout. Once you learn it, you will feel at home in any Spring Boot project you encounter.
At the top level of your contact-api folder, you will see several files and directories. Let us go through the most important ones.
pom.xml — This is the most important configuration file in your project. POM stands for "Project Object Model," and this file tells Maven everything it needs to know about your project: its name, its version, and most critically, its dependencies (the external libraries it needs). If you have done any JavaScript development, think of pom.xml as the Java equivalent of package.json. When you selected "Spring Web" on start.spring.io, it added an entry to this file. Maven reads this file and automatically downloads all the required libraries for you.
src/main/java — This is where all of your Java source code lives. Inside this directory, you will find a folder structure that matches your package name: org/codersfarm/contactapi/. Every Java class you write for this project will go somewhere inside this directory. This is where you will spend most of your time.
src/main/resources — This directory holds configuration files and other non-Java resources. The most important file here is application.properties (or application.yml). This is where you configure your application's settings, like which port the web server should run on, database connection details, and other runtime options. Right now, this file is empty, and that is perfectly fine — Spring Boot's defaults work great out of the box.
src/test/java — This is where your test code goes. Spring Initializr even generated a basic test file for you. We will not focus on testing in this lesson, but it is good to know that Spring Boot projects have a dedicated place for tests right from the start.
mvnw and mvnw.cmd — These are the Maven Wrapper scripts. The mvnw file is for Mac and Linux, and mvnw.cmd is for Windows. These scripts let you run Maven commands without having Maven installed globally on your computer. The wrapper automatically downloads and uses the correct version of Maven for your project. This is extremely convenient and ensures everyone on a team uses the same Maven version.
Now let us look at the most important file in the entire project — the main application class. Open the file at src/main/java/org/codersfarm/contactapi/ContactApiApplication.java:
package org.codersfarm.contactapi;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ContactApiApplication {
public static void main(String[] args) {
SpringApplication.run(ContactApiApplication.class, args);
}
}
This tiny file is the entry point for your entire application. Let us break it down line by line.
The package line at the top declares which package this class belongs to. The two import statements bring in classes from the Spring Boot library that we need. Then we have the class declaration itself: public class ContactApiApplication. This is just a regular Java class with a main method — the same main method you have seen in every Java program.
The magic is in the @SpringBootApplication annotation sitting above the class. This single annotation does three important things behind the scenes: it enables auto-configuration (Spring Boot automatically sets up your application based on the dependencies you added), it enables component scanning (Spring Boot automatically finds and registers all of your classes that have special annotations), and it marks this class as a configuration source. You do not need to memorize all of that right now. Just remember: @SpringBootApplication is the annotation that tells Spring Boot "this is where my application starts, please set everything up for me."
Inside the main method, the single line SpringApplication.run(ContactApiApplication.class, args) starts the entire Spring Boot application. It boots up the embedded Tomcat web server, applies all the auto-configuration, scans for your code, and gets everything ready to handle web requests. All of that happens in one line.
@ symbols you see above classes and methods (like @SpringBootApplication). They are special markers that provide extra information about your code. Spring Boot reads these annotations and uses them to automatically configure your application. You will see many more annotations throughout this track.
Part 4 — Your First Endpoint
Now it is time to build something you can actually see in your browser. You are going to create your first REST endpoint — a URL that your application responds to. When someone visits that URL, your application will send back a message. This is the fundamental building block of every web API.
In Spring Boot, you create endpoints by writing a special kind of class called a controller. A controller is a class whose job is to handle incoming web requests and send back responses. Think of it like a receptionist at an office — when a visitor (a web request) arrives, the receptionist (the controller) figures out what they need and gives them an answer (the response).
Create a new file inside the src/main/java/org/codersfarm/contactapi/ folder and name it HelloController.java. Type the following code into that file:
package org.codersfarm.contactapi;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@GetMapping("/hello")
public String sayHello() {
return "Hello from Spring Boot!";
}
}
This is just 13 lines of code, but there is a lot happening here. Let us go through every part.
@RestController — This annotation sits above the class declaration and tells Spring Boot two things. First, this class is a controller — it handles web requests. Second, the "Rest" part means that whatever your methods return will be sent directly as the response body. In other words, if your method returns a String, that exact string is what the person making the request will see. Without this annotation, Spring Boot would not know that this class is supposed to handle web traffic.
@GetMapping("/hello") — This annotation sits above the sayHello method and tells Spring Boot: "When someone sends a GET request to the path /hello, run this method." The word "GET" refers to the HTTP method — it is the same type of request your browser makes every time you visit a web page. The "/hello" part is the URL path. So when you combine your server address with this path, you get http://localhost:8080/hello.
public String sayHello() — This is a regular Java method. It takes no arguments and returns a String. Because this class is annotated with @RestController, Spring Boot will take whatever this method returns and send it back as the HTTP response. In this case, anyone who visits /hello will see the text "Hello from Spring Boot!" in their browser.
HelloController.java file is in the same package as your ContactApiApplication.java file (or a sub-package of it). Spring Boot's component scanning starts at the package where @SpringBootApplication lives and scans downward. If your controller is in a completely different package, Spring Boot will not find it, and your endpoint will not work.
Now let us run the application and see it in action. Open a terminal and navigate to your project's root folder (the contact-api directory that contains the pom.xml file). Then run one of the following commands depending on your operating system:
Mac or Linux:
./mvnw spring-boot:run
Windows:
mvnw.cmd spring-boot:run
The first time you run this, it may take a minute or two. Maven needs to download dependencies and compile your code. You will see a lot of text scrolling in the terminal. Watch for a line near the end that says something like:
Tomcat started on port 8080 (http) with context path '/'
Started ContactApiApplication in 2.345 seconds
When you see that message, your application is running. The embedded Tomcat web server is live and listening for requests on port 8080. Now open your web browser and navigate to:
http://localhost:8080/hello
You should see the text "Hello from Spring Boot!" displayed in your browser. Congratulations — you have just built and run your first Spring Boot endpoint! That text came from your sayHello() method inside HelloController.java.
Let us take a moment to appreciate what just happened. You wrote a Java class with a single method, added two annotations, ran a command, and now you have a live web server responding to HTTP requests. No XML configuration files. No installing and configuring Tomcat separately. No complicated deployment steps. This is the power of Spring Boot.
Want to try something? While the application is still running, try visiting http://localhost:8080/ (without the /hello path). You will see an error page that says "Whitelabel Error Page" with a 404 status. This is because you only defined one endpoint at /hello. Spring Boot does not know what to do with a request to /, so it returns a "Not Found" error. This is expected behavior and shows that your application is only responding to the routes you have explicitly defined.
Part 5 — How It All Connects
Now that you have seen it work, let us slow down and trace the complete journey of a request from your browser to your code and back. Understanding this flow is crucial because every single web application — whether it is a simple portfolio site or a massive e-commerce platform — follows this same basic pattern.
The Request/Response Flow
- You type a URL in your browser. When you enter
http://localhost:8080/helloand press Enter, your browser creates an HTTP GET request and sends it tolocalhost(your own computer) on port8080. - The embedded Tomcat server receives the request. Spring Boot's built-in Tomcat web server is listening on port 8080. It accepts the incoming request and passes it along to the Spring framework for processing.
- Spring routes the request to the correct controller. Spring looks at the request path (
/hello) and the HTTP method (GET). It searches through all of your controller classes for a method annotated with@GetMapping("/hello"). It finds a match in yourHelloControllerclass. - Your method runs. Spring calls the
sayHello()method. The method executes and returns the String"Hello from Spring Boot!". - Spring sends the response back. Because the class is marked with
@RestController, Spring takes the returned String, wraps it in an HTTP response with a 200 (OK) status code, and sends it back through Tomcat to your browser. - Your browser displays the result. The browser receives the response and renders the text on screen. The entire round trip typically takes just a few milliseconds.
This six-step flow — request in, route to controller, execute method, response out — is the heartbeat of every Spring Boot application. Every feature you build in the future will be a variation of this pattern. Sometimes the method will read data from a database before returning. Sometimes it will accept data from the request body (like a contact form submission). Sometimes it will return JSON instead of plain text. But the underlying flow is always the same.
It is worth noting how much Spring Boot is doing for you behind the scenes. Without Spring Boot, you would have to manually configure Tomcat, write code to parse HTTP requests, set up a routing system, handle serialization, manage error responses, and much more. Spring Boot handles all of this automatically, letting you focus on the code that matters — your business logic inside the controller methods.
Let us put this in the context of what you are building. Remember the contact form from the web basics lessons? When a user fills out that form and clicks "Submit," the browser will send an HTTP POST request to your Spring Boot server. Your server will have a controller that receives the form data (name, email, message), validates it, stores it, and sends back a response confirming the submission. That is exactly the same pattern you just learned, just with a few more steps.
Right now, your HelloController only has one endpoint. But a real application will have many controllers, each responsible for a different part of your API. You might have a ContactController for handling contact form submissions, a UserController for managing user accounts, and so on. Spring Boot makes it easy to organize your code this way.
In the next lesson, you will take what you learned today and build something real: a Contact Form API. You will create an endpoint that accepts POST requests with JSON data, validates the input, and sends back a structured response. That is a huge step toward making your contact form fully functional. Everything you learned in this lesson — the project setup, the annotations, the request flow — is the foundation you will build on.
HelloController. For example, add a method with @GetMapping("/goodbye") that returns "Goodbye from Spring Boot!". Restart your application and visit http://localhost:8080/goodbye in your browser. Getting comfortable adding endpoints is the best practice you can do right now.
Quiz — Check Your Understanding
1. What does Spring Boot provide that plain Java does not?
Correct answer: B. Spring Boot provides auto-configuration (it automatically sets up your application based on your dependencies) and an embedded web server (Tomcat), so you do not need to install or configure these things yourself. Plain Java does not include any of these features out of the box.
2. What annotation marks a class as a REST API controller?
Correct answer: C. The @RestController annotation tells Spring Boot that this class handles incoming HTTP requests and that the return values of its methods should be sent directly as response bodies. This is what you use when building REST APIs.
3. What URL would you visit to see the output of @GetMapping("/hello")?
Correct answer: B. Spring Boot's embedded Tomcat server runs on port 8080 by default. The @GetMapping("/hello") annotation maps GET requests at the /hello path, so the full URL is http://localhost:8080/hello.
Finished this lesson?