Spring Boot Basics: Build Your First Java Web App
What Is Spring Boot?
In this lesson you will
- Explain what Spring Boot adds on top of the Spring Framework
- Generate and run a project from Spring Initializr
- Write and call a first REST endpoint
Introduction
Spring Boot is the most widely used way to build Java back-end services. It sits on top of the Spring Framework, which provides the core ideas (objects managed for you, web support, data access), and removes most set-up work. You choose what you need, and Boot configures sensible defaults and includes a web server inside your application.
You should know basic Java (classes, methods, interfaces, records) before this course. Java 17 or newer is required, and Java 21 is a good choice.
A note on verification. This course was written for Spring Boot 4.1.2. I generated a real project from Spring Initializr and checked the starter names, package names and configuration property names against the 4.1.2 release artifacts. I could not compile or run the Spring examples in the environment used to write this course, so treat the code as carefully written but not executed. Spring changes between major versions (Boot 4 changed some package names and starters compared with Boot 3), so check the official documentation if an import fails.
What Boot gives you
- Starters: one dependency pulls in a compatible set of libraries, such as a web server and JSON support.
- Auto-configuration: Boot looks at what is on your classpath and configures it. Add a database driver and Boot sets up a data source.
- Embedded server: your app is a normal Java program with a web server (Tomcat by default) inside. Run it with
java -jar. - Production features: health checks, metrics and externalised configuration.
Generate a project
Open https://start.spring.io and choose:
- Project: Maven, Language: Java
- Spring Boot: the latest stable release (this course uses 4.1.2)
- Group and artifact: for example
com.exampleanddemo - Java: 21
- Dependencies: Spring Web (and later Validation, Spring Data JPA, H2 Database, Spring Security, Actuator)
Press Generate, unzip the file and open it in an IDE. When I generated a project with Web, Validation, Data JPA, H2, Actuator and Security selected, the build file listed starters named like spring-boot-starter-webmvc and spring-boot-starter-validation. In Spring Boot 4 the web starter is named spring-boot-starter-webmvc, and each starter has a matching test starter such as spring-boot-starter-webmvc-test.
The generated structure
demo/
mvnw, mvnw.cmd Maven wrapper (no need to install Maven)
pom.xml build file: dependencies and plugins
src/main/java/com/example/demo/DemoApplication.java
src/main/resources/application.properties
src/test/java/com/example/demo/DemoApplicationTests.javaThe generated main class is:
Java
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}@SpringBootApplication turns on auto-configuration and component scanning, which finds your annotated classes in this package and below. Keep your other classes in sub-packages of com.example.demo, or Spring will not find them.
The generated application.properties contains just spring.application.name=demo.
Your first endpoint
Create HelloController.java in the same package:
Java
package com.example.demo;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@GetMapping("/hello")
public String hello() {
return "Hello, Spring Boot";
}
}@RestController marks a class whose methods handle web requests and return data directly. @GetMapping("/hello") maps GET requests for /hello to the method.
Run it
Terminal
./mvnw spring-boot:runOn Windows use mvnw.cmd spring-boot:run. The first run downloads dependencies, which can take a few minutes. You should see a log line that the application started, and the server listens on port 8080 by default. Then:
Terminal
curl http://localhost:8080/helloshould print Hello, Spring Boot. (This is the expected result, not one I ran.)
Because the generated project can include Spring Security, as in my sample, all endpoints may require a login by default. Security is covered later. For now, generate the project with only Spring Web, so /hello is open.
Quick quiz
What does @SpringBootApplication enable?
Spring versus Spring Boot
- Spring Framework: the foundation: dependency injection, web MVC, data access, testing support.
- Spring Boot: conventions and auto-configuration on top, so you start quickly.
You will use both without noticing the boundary.
Try it yourself
Generate a project with Spring Web only. Add the HelloController, run it and visit /hello. Add a second endpoint /about that returns the application name.
Summary
- Spring Boot adds starters, auto-configuration and an embedded server to the Spring Framework.
- Generate a project at start.spring.io, keep your classes under the main package and run with the Maven wrapper.
- A class with @RestController and a method with @GetMapping makes your first endpoint.
Lesson complete
+20 XP
Nice work. Keep going while it is fresh.
Sources and further reading