Skip to main content
PreparationZone Logo
PrepZone

Your First Spring Boot Application

Generate a project at start.spring.io, run it, and understand what Boot set up for you.

Pass checkpoint below

Why this matters

  • Generating and running your first project proves the toolchain works before you add BookStore features.
  • Understanding what @SpringBootApplication enables explains later modules on auto-configuration and component scanning.
  • The Initializr defaults (Java 21, Maven, Jar packaging) are the baseline every article in this track assumes.

@SpringBootApplication entry point

L1 · Fresher

SpringApplication.run starts the context, runs auto-configuration, and launches embedded Tomcat. One public class with main is enough for BookStore to listen on port 8080.

What Initializr selected for you

L2 · Intermediate

start.spring.io generates Maven/Gradle, Java 21, Boot 3.5, and starters such as spring-boot-starter-web. Each starter pulls a tested dependency graph from the BOM.

Actuator and devtools on day one

L3 · Advanced

Add spring-boot-starter-actuator for health endpoints; add DevTools only in dev scope for fast restart. Never ship DevTools to production images.

Inspecting what Boot configured

L4 · Expert

Enable debug=true or the conditions report to see which @AutoConfiguration classes matched. This is how you prove in interviews that you understand defaults vs overrides.

bookstore/
src/main/java/.../BookStoreApplication.java@SpringBootApplication entry point
src/main/java/.../controller/REST endpoints
src/main/java/.../service/Business logic
src/main/java/.../repository/Data access
src/main/resources/application.ymlConfiguration
pom.xmlMaven dependencies
Source code lives under src/main/java. Configuration and static assets sit in src/main/resources.

Generate the project

Visit start.spring.io with these settings:

Initializr choices for BookStore

  • Project — Maven
  • Language — Java
  • Spring Boot — 3.5.x
  • Java — 21
  • Dependencies — Spring Web

Download, unzip, and open in your IDE. The generated BookStoreApplication looks like:

Java
Loading compiler…

Add a health endpoint

Create controller/HealthController.java:

Java
// learn:fragment
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Map;
package com.example.bookstore.controller;



@RestController
public class HealthController {

    @GetMapping("/health")
    public Map<String, String> health() {
        return Map.of("status", "UP", "service", "bookstore-api");
    }
}

@RestController combines @Controller and @ResponseBody — return values serialise directly to JSON.

Run the application

Java
./mvnw spring-boot:run

Console output shows Tomcat starting on port 8080:

Java
Tomcat started on port 8080 (http) with context path '/'
Started BookStoreApplication in 1.8 seconds

Verify with curl:

Java
curl http://127.0.0.1:8080/health
# {"status":"UP","service":"bookstore-api"}

What Boot set up for you

Without writing configuration, Boot provided:

Auto-configured defaults

  • Embedded Tomcat on port 8080
  • Jackson JSON serialisation for return types
  • Component scanning in com.example.bookstore and sub-packages
  • A default error page for unmapped URLs
  • Logging via Logback with sensible defaults

application.yml basics

Replace application.properties with YAML for readability:

Java
spring:
  application:
    name: bookstore-api

server:
  port: 8080

logging:
  level:
    com.example.bookstore: DEBUG

Spring binds spring.application.name to the process name in logs and metrics.

The startup sequence

When SpringApplication.run() executes:

Startup sequence

  1. Creates an ApplicationContext.
  2. Registers @SpringBootApplication as a configuration source.
  3. Runs auto-configuration based on classpath (web starter → DispatcherServlet).
  4. Scans for @Component classes under the main class package.
  5. Starts the embedded web server.
  6. Publishes ApplicationReadyEvent.

Failures at any step print a clear stack trace — read from the bottom up to find the root cause.

Project structure after first endpoint

Java
bookstore-api/
├── src/main/java/com/example/bookstore/
│   ├── BookStoreApplication.java
│   └── controller/HealthController.java
├── src/main/resources/application.yml
├── src/test/java/.../BookStoreApplicationTests.java
└── pom.xml

The generated test uses @SpringBootTest to verify the context loads:

Java
// learn:fragment
@SpringBootTest
class BookStoreApplicationTests {
    @Test
    void contextLoads() { }
}

Common first-run problems

Fixes that unblock beginners

  • Port 8080 in use — Set server.port=8081 or kill the conflicting process.
  • Java version mismatch — Boot 3.x requires Java 21; run java -version to confirm.
  • Controller not found (404) — Ensure the controller package is under com.example.bookstore, not a sibling package outside the scan root.
  • Build fails on javax imports — You are on Boot 3; use jakarta.* packages.

Package the JAR

Java
./mvnw clean package
java -jar target/bookstore-api-0.0.1-SNAPSHOT.jar

The same health endpoint responds — proof the fat JAR is deployment-ready.

Quick recall

Everything you need if you only revisit this box.

  • Generate projects at start.spring.io with Java 21, Maven, and the Web starter.
  • @SpringBootApplication enables auto-config, component scanning, and configuration registration.
  • SpringApplication.run() bootstraps the context and starts embedded Tomcat.
  • Controllers belong under the main class package so scanning finds them.
  • application.yml externalises port, logging, and application name.
  • ./mvnw package produces a self-contained JAR for deployment.

Test yourself

Answer these before moving on — recall is what makes it stick.

Checkpoint quiz

Pass 6 of 9 to verify this article on your path — scrolling alone does not count.

Question 1 of 9

BookStore on-call: a junior says "@SpringBootApplication entry point" is only theory. What do you correct them with?