Learn to Code
All tracks
Learn to Code

Free lessons from scratch to pro — every language, databases and DevOps.

AboutContactPrivacy PolicyTerms of Use

Your progress is saved in this browser. © 2026 Learn to Code

SB
Spring Boot
0/10 completed
Beginner
Spring Boot setupControllers and RESTBeans and dependency injection
Intermediate
Spring Data JPAValidation and error handlingTesting Spring Boot apps
Advanced
Spring SecurityRelationships, paging and migrationsMicroservices with Spring
Pro
Production-ready Spring Boot
Tracks / Spring Boot / Beginner

Spring Boot setup

Spring is the most widely used framework for Java backends: banks, airlines, retailers and streaming services run on it. Spring Boot is the fast way to use Spring: it picks sensible defaults, configures libraries automatically, and packages your whole app, web server included, into one runnable JAR. This track uses Spring Boot 4 with Java 25.

Java doesn't run in the browser, so this track shows each program's real output under the code. Every example was built and run with Spring Boot 4.2.0 on Java 25, and every curl response shown is exactly what the app returned. Install a JDK (Java 21 or newer; 25 is the current LTS) and an IDE such as IntelliJ IDEA or VS Code to follow along.

Creating a project

Go to start.spring.io (Spring Initializr), or use the "New Spring Boot project" wizard in your IDE, and choose:

  • Project: Maven (or Gradle), Language: Java, Spring Boot: 4.2.x, Java: 25
  • Dependencies: Spring Web, Spring Data JPA, H2 Database, Validation, Spring Boot Actuator

It generates this structure:

shop/
├── pom.xml                                  dependencies and build settings
├── mvnw, mvnw.cmd, .mvn/                    the Maven wrapper (no Maven install needed)
└── src/
    ├── main/
    │   ├── java/com/example/shop/
    │   │   └── ShopApplication.java         the entry point
    │   └── resources/
    │       └── application.properties       configuration
    └── test/java/com/example/shop/
        └── ShopApplicationTests.java

The pom.xml

xml
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>4.2.0</version>
</parent>

<properties>
    <java.version>25</java.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-webmvc</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-validation</artifactId>
    </dependency>
    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-webmvc-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>
  • The parent fixes the versions of hundreds of libraries that are tested together, so you don't write version numbers for them.
  • Starters are curated bundles: spring-boot-starter-webmvc brings Spring MVC, an embedded Tomcat server and Jackson for JSON. In Spring Boot 4 each technology has its own starter and its own test starter (…-webmvc-test, …-data-jpa-test). (Older projects use spring-boot-starter-web, which still works.)

The entry point

java
package com.example.shop;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ShopApplication {

    public static void main(String[] args) {
        SpringApplication.run(ShopApplication.class, args);
    }
}

@SpringBootApplication combines three things:

Annotation Does
@SpringBootConfiguration This class can define beans (objects Spring manages)
@EnableAutoConfiguration Configure everything the dependencies need (a web server because Spring MVC is present, a database connection because JPA and H2 are present)
@ComponentScan Find your classes annotated with @RestController, @Service, @Component… in this package and below

Keep ShopApplication in the root package (com.example.shop) so the scan finds everything in sub-packages.

Running it

bash
./mvnw spring-boot:run          # run from source (Windows: mvnw.cmd spring-boot:run)
./mvnw package                  # build target/shop-0.0.1-SNAPSHOT.jar
java -jar target/shop-0.0.1-SNAPSHOT.jar

The start-up log tells you what happened (shortened here):

Output
Starting ShopApplication v0.0.1-SNAPSHOT using Java 25.0.5.1 with PID 67386
No active profile set, falling back to 1 default profile: "default"
Starting service [Tomcat]
Starting Servlet engine: [Apache Tomcat/11.0.27]
HikariPool-1 - Starting...
Tomcat started on port 8080 (http) with context path '/'
Started ShopApplication in 1.917 seconds (process running for 2.115)

An embedded Tomcat web server started, a database connection pool (HikariCP) connected to the in-memory H2 database, and the app is listening on port 8080. There's no separate server to install: the JAR is the server.

Your first endpoint

java
package com.example.shop.greeting;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class GreetingController {

    @GetMapping("/hello")
    public String hello(@RequestParam(defaultValue = "world") String name) {
        return "Hello, " + name + "!";
    }
}
bash
curl localhost:8080/hello
curl "localhost:8080/hello?name=Asha"
Output
Hello, world!
Hello, Asha!

Spring found the class (component scanning), saw @RestController, and mapped GET /hello to the method.

Configuration: application.properties

Settings live in src/main/resources/application.properties (or application.yml):

properties
spring.application.name=shop
server.port=8080
spring.jpa.open-in-view=false
logging.level.org.hibernate.SQL=debug

Every setting can be overridden without rebuilding, which is how the same JAR runs everywhere:

bash
java -jar shop.jar --server.port=9090                 # command-line argument
SERVER_PORT=9090 java -jar shop.jar                    # environment variable (relaxed binding)
SPRING_PROFILES_ACTIVE=prod java -jar shop.jar         # load application-prod.properties too

Profiles group environment-specific settings: application-dev.properties, application-prod.properties, activated with spring.profiles.active.

Developer tools

Add spring-boot-devtools (development only) for automatic restarts when you recompile, and use your IDE's run button for the main class. In IntelliJ IDEA, the Spring plugin shows every endpoint and bean.

Practice

  1. Generate a project on start.spring.io with Spring Web, run it, and call /hello.
  2. Change the port to 9090 in application.properties, then override it back to 8080 from the command line.
  3. Add a GET /time endpoint that returns java.time.Instant.now().toString().
✎ Check your understanding

1.What does a Spring Boot "starter" provide?

2.Which annotation enables auto-configuration and component scanning?

3.How do you run the same JAR on a different port without rebuilding?