Learn Spring Boot with Java: create a backend, build REST APIs, validate data, connect a database, write tests and prepare a secure application for deployment. Every module explains the idea in plain language, walks through a code example and gives you a small task to try yourself.
Version snapshot: Spring Boot 4.1.2 · stable. Spring Boot 4.1.2 is the latest stable release checked on 24 October 2026. Spring Boot 4.2.0-RC1 is a preview, so this guide uses the stable line. Boot 4.1 requires Java 17 or later and supports Java up to 27. Confirm build-tool, library and hosting compatibility before starting a real project.
Before you begin
Start with the TCM Java guide first if classes, methods, collections, exceptions and Maven or Gradle are new. You need a JDK 17 or newer, a code editor and an internet connection to create the starter project. Java 25 is an LTS option; use the JDK supported by your libraries and deployment platform.
What you’ll be able to do
Create a maintainable Java REST API with clear controller, service and repository roles; persist data, validate requests, test behavior, configure environments and package an executable application.
New to Java classes, methods or collections? Start with the Java foundations guide, then return here to build your backend.
Work through the modules in order if you’re new to Spring Boot. If you already know the basics, use the contents list to jump to a topic. Try each example by changing one value, running it again and explaining the result in your own words.
Spring Boot terms in simple language
Spring Boot
A Java framework that starts applications with useful defaults, dependency starters and an embedded server.
Spring Framework
The underlying Java toolkit for connecting application objects and handling common application work.
Bean
An application object that Spring creates, configures and can provide to other objects.
Dependency injection
Giving a class the collaborators it needs instead of making it secretly construct them itself.
Auto-configuration
Spring Boot’s conditional setup of common infrastructure based on the libraries and settings in a project.
Starter
A curated dependency that brings in a related set of libraries for a common job, such as web MVC.
REST endpoint
An address and HTTP method that an API uses to read or change a resource.
DTO
A data transfer object that defines the information an API accepts or returns.
Repository
A component that provides a clear interface for reading and writing persisted data.
Profile
A named configuration choice for changing application settings between development, testing and production.
Java is the language; Spring Framework provides tools for connecting application objects and handling web requests; Spring Boot adds sensible defaults, dependency starters and an embedded server so you can start a Java backend with less setup. Boot does not replace Java fundamentals. It organizes them into an application that can start on its own, usually with java -jar. First learn what a request, response, class, method and exception are, then Spring annotations will feel like readable instructions instead of magic.
Follow this example
Java source code
↓ compile
Java bytecode + Spring libraries
↓ SpringApplication.run(...)
Embedded web server listens for HTTP requests
↓ controller handles a request
JSON response returned to the caller
Try it yourself: In your own words, explain the difference between Java, Spring Framework and Spring Boot. Name one job an embedded server does.
MODULE 02 / 14 · CONCEPT EXPLAINED
Create a project with Spring Initializr
Spring Initializr at start.spring.io generates the build files and folder structure for you. Choose Maven or Gradle, Java, a stable Spring Boot 4.1.x version, a clear group and artifact name, and the Spring Web dependency. In Boot 4 the MVC starter is spring-boot-starter-webmvc. Generate and unzip the project, then open the folder containing pom.xml or build.gradle. Use an IDE that imports Maven or Gradle so it can download dependencies and show compile errors.
Follow this example
Project choices
Project: Maven
Language: Java
Spring Boot: latest stable 4.1.x
Group: in.tcm.learning
Artifact: study-api
Dependency: Spring Web (MVC)
# Confirm the installed JDK
java -version
Try it yourself: Generate a starter project named study-api and identify the build file, main source folder and test folder.
MODULE 03 / 14 · CONCEPT EXPLAINED
Read the project structure and build file
The build file declares the Spring Boot version and dependencies. Maven uses pom.xml; Gradle uses build.gradle or build.gradle.kts. The build tool downloads the libraries, compiles Java and packages the app. Keep your main application class in a root package such as in.tcm.learning so component scanning can find subpackages like controller and service. Do not manually copy random jar files into the project; declare dependencies in the build file so they can be reproduced on another computer.
Try it yourself: Find the Boot parent or plugin and the web MVC starter in your generated build file. Explain why src/main/java and src/test/java are separate.
MODULE 04 / 14 · CONCEPT EXPLAINED
Start the application and understand auto-configuration
@SpringBootApplication combines configuration, auto-configuration and component scanning. Auto-configuration looks at the libraries you included and prepares common infrastructure when suitable configuration is missing. For example, the MVC starter gives Boot enough information to configure a web application and embedded Tomcat. It is a starting default, not a rule you can never change. The main method calls SpringApplication.run, which builds the application context and starts the server.
Follow this example
package in.tcm.learning;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class StudyApiApplication {
public static void main(String[] args) {
SpringApplication.run(StudyApiApplication.class, args);
}
}
Try it yourself: Run the generated project with ./mvnw spring-boot:run (Windows: mvnw.cmd spring-boot:run) and locate the startup message that shows the server port.
MODULE 05 / 14 · CONCEPT EXPLAINED
Build your first REST endpoint
A REST endpoint is an HTTP method and path that your application understands. @RestController marks a class as a web handler whose return values are written to the response body. @GetMapping maps a GET request to a method. A GET should retrieve information and should not secretly change server data. With a JSON library on the classpath, returning a Java object can produce JSON; returning plain text produces a simple text response.
Follow this example
package in.tcm.learning.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@GetMapping("/api/hello")
public String hello() {
return "Hello from Spring Boot";
}
}
Try it yourself: Add GET /api/hello, run the app and open http://localhost:8080/api/hello. Note the difference between the URL path and the Java method name.
MODULE 06 / 14 · CONCEPT EXPLAINED
Request mappings, path variables and query parameters
HTTP methods communicate the kind of action: GET reads, POST creates, PUT replaces, PATCH partially changes, and DELETE removes. A path variable identifies a resource in the route, such as /api/lessons/7. A query parameter adds optional filtering or search information, such as ?level=beginner. Use specific mapping annotations so the intended method is clear, and choose resource-oriented paths that remain readable as the API grows.
Follow this example
@RestController
@RequestMapping("/api/lessons")
public class LessonController {
@GetMapping("/{id}")
public String byId(@PathVariable long id) {
return "Lesson " + id;
}
@GetMapping
public String byLevel(@RequestParam(defaultValue = "all") String level) {
return "Level: " + level;
}
}
Try it yourself: Try /api/lessons/7 and /api/lessons?level=beginner. Then add a second query parameter for a search term.
MODULE 07 / 14 · CONCEPT EXPLAINED
DTOs, JSON and request validation
A DTO (data transfer object) is a simple shape for information that crosses the API boundary. It keeps the public request format separate from internal database entities. A record is useful for an immutable request or response shape. Bean Validation annotations describe rules; @Valid asks Spring to apply those rules to an incoming request. In Boot 4, add the validation starter from Initializr or your build file. Invalid input should produce a useful 400 response, and server-side checks are essential even if a browser form also validates.
Follow this example
import jakarta.validation.constraints.NotBlank;
public record CreateLessonRequest(
@NotBlank(message = "Title is required") String title,
@NotBlank(message = "Level is required") String level
) {}
@PostMapping
public LessonResponse create(@Valid @RequestBody CreateLessonRequest request) {
return lessonService.create(request);
}
Try it yourself: Create a request record with a required title and a suitable length rule. Send valid and invalid JSON and compare the response status.
MODULE 08 / 14 · CONCEPT EXPLAINED
Dependency injection and controller-service-repository layers
Spring creates and connects application objects called beans. Dependency injection means a class receives the objects it needs rather than constructing hidden dependencies with new. Keep the controller focused on HTTP: read the request and choose a response. Put application rules in a service. Put persistence operations behind a repository. Constructor injection makes dependencies visible, supports immutable fields and makes classes easier to test. Do not add a layer just for ceremony; each layer should have a clear job.
Follow this example
@Service
public class LessonService {
private final LessonRepository repository;
public LessonService(LessonRepository repository) {
this.repository = repository;
}
public List<Lesson> findAll() {
return repository.findAll();
}
}
Try it yourself: Move a piece of application logic out of a controller into a service and inject the service through its constructor.
MODULE 09 / 14 · CONCEPT EXPLAINED
Persist data with Spring Data JPA
A database keeps information after the application stops. JPA maps Java entity objects to relational tables, while Spring Data can create common repository methods from an interface. Start locally with an in-memory database to learn the mapping, then use a real database and deliberate schema migrations for production. Keep API DTOs separate from entities so a database refactor does not accidentally change your public JSON contract. Use transactions around related writes and understand when they commit or roll back.
Follow this example
@Entity
public class Lesson {
@Id @GeneratedValue
private Long id;
private String title;
protected Lesson() {} // JPA needs a constructor
public Lesson(String title) { this.title = title; }
public Long getId() { return id; }
public String getTitle() { return title; }
}
public interface LessonRepository extends JpaRepository<Lesson, Long> {}
Try it yourself: Add the JPA and H2 dependencies, save one lesson through a repository, restart the app and observe how an in-memory database behaves.
MODULE 10 / 14 · CONCEPT EXPLAINED
Predictable errors and API responses
A useful API distinguishes success from failure with HTTP status codes: 200 for a successful read, 201 for a created resource, 400 for invalid input, 404 for a missing resource and 500 for an unexpected server problem. Do not return success-shaped text for failures. Handle expected domain cases deliberately and use a centralized exception handler when multiple endpoints need the same error shape. Never expose stack traces, SQL, secrets or internal implementation details to callers.
Follow this example
@RestControllerAdvice
public class ApiErrorHandler {
@ExceptionHandler(LessonNotFoundException.class)
ResponseEntity<Map<String, String>> notFound(LessonNotFoundException error) {
return ResponseEntity.status(HttpStatus.NOT_FOUND)
.body(Map.of("error", error.getMessage()));
}
}
Try it yourself: Make a request for a lesson ID that does not exist and return a clear 404 response with a small JSON error body.
MODULE 11 / 14 · CONCEPT EXPLAINED
Test the application at more than one level
A unit test checks one class with controlled dependencies; it is quick and helps verify a rule. A web slice test checks controller mapping and JSON behavior without starting every part of the application. An integration test checks that configured components work together. Spring Boot provides @SpringBootTest for loading an application context, and spring-boot-starter-test brings common test tools. Test both expected results and important edge cases; do not make every small test load the entire application.
Follow this example
@SpringBootTest
class StudyApiApplicationTests {
@Test
void applicationContextStarts() {
// If Spring cannot build the application context, this test fails.
}
}
// Run the suite with: ./mvnw test
Try it yourself: Write a service unit test for one business rule and a web test that checks the status and JSON returned by an endpoint.
MODULE 12 / 14 · CONCEPT EXPLAINED
Configuration, profiles and environment variables
Configuration changes between a laptop, test environment and production. Keep non-secret defaults in application.properties or application.yml. Use profiles for environment-specific behavior, for example application-dev.properties. Supply passwords and API keys through environment variables or a secret manager, never commit real credentials. Bind related settings into a configuration-properties class as an application grows, so configuration has a clear type and can be validated at startup.
Follow this example
# application.properties
server.port=${PORT:8080}
spring.application.name=study-api
# Example environment variable (set outside source control)
SPRING_DATASOURCE_PASSWORD=use-a-local-secret
# Start with a profile when needed
java -jar app.jar --spring.profiles.active=dev
Try it yourself: Move the server port to an environment variable with a safe local default. Confirm a production password is not present in any tracked file.
MODULE 13 / 14 · CONCEPT EXPLAINED
Security basics for a backend API
A working endpoint is not automatically a secure endpoint. Decide which routes are public and which require authentication. Use Spring Security rather than writing your own password checks, hash passwords with a suitable adaptive algorithm, validate authorization on the server and use HTTPS in production. CORS controls which browser origins may read responses; it is not user authentication. Keep dependencies updated, give the application only the permissions it needs, and avoid returning private fields in DTOs.
Try it yourself: Draw a route table that marks public and protected endpoints. Add Spring Security and verify an unauthenticated request is rejected before adding a real login flow.
MODULE 14 / 14 · CONCEPT EXPLAINED
Build, run and deploy a Spring Boot application
Before release, run the complete build so compilation and automated checks happen together. The Spring Boot build plugin creates an executable jar with the application and its required libraries. You can run that file with java -jar. A production plan also needs environment configuration, a database migration strategy, HTTPS termination, logs, health monitoring, backups and a rollback path. Start with a managed Java host or container platform and follow its current supported JDK instructions; a successful local run alone does not prove production readiness.
Follow this example
# Maven: build and run the executable jar
./mvnw clean verify
java -jar target/study-api-0.0.1-SNAPSHOT.jar
# Gradle equivalent
./gradlew clean build
java -jar build/libs/study-api-0.0.1-SNAPSHOT.jar
Try it yourself: Package your project, run the jar on a non-default port, document the environment variables it needs, and write down how you would restore the previous release.
PUT THE MODULES TOGETHER
Your final project
Build a Java study-planner REST API with create/read/update/delete endpoints, request validation, a database, tests, environment-based configuration and a deployment README. Write down what the project should do, build it in small steps, and include a README that explains how to run it.
Use primary documentation for current language rules and version changes. The examples in this guide are learning starting points; verify details for the exact runtime and browser support your project needs.
Yes, learn basic Java classes, methods, collections, exceptions and a build tool first. The TCM Java guide gives you that foundation; then this guide shows how Spring Boot wires Java code into a backend application.
Which Spring Boot version does this guide use?
This guide follows Spring Boot 4.1.2, the latest stable release checked on 24 October 2026. Spring Boot 4.2.0-RC1 is a preview release. Check the official Spring documentation for updates before starting a project.
What Java version do I need?
Spring Boot 4.1 requires Java 17 or later and supports Java through 27. For a longer support window, Java 25 is an LTS release; confirm your libraries and hosting provider support the JDK you select.
Is Spring Boot only for REST APIs?
No. It can also serve HTML pages, run scheduled jobs, process messages and support other application styles. A REST API is a useful first project because its requests and responses are easy to inspect.