PrepZone Logo
PrepZone

Your First Spring Boot Application

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

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 17, Maven, Jar packaging) are the baseline every article in this track assumes.
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.4.x
  • Java — 17
  • Dependencies — Spring Web

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

Java
package com.example.bookstore;

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

@SpringBootApplication
public class BookStoreApplication {

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

Add a health endpoint

Create controller/HealthController.java:

Java
package com.example.bookstore.controller;

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

import java.util.Map;

@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
@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 17; 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 17, 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.