Why this matters
- Generating and running your first project proves the toolchain works before you add BookStore features.
- Understanding what
@SpringBootApplicationenables 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.
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:
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:
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
./mvnw spring-boot:run
Console output shows Tomcat starting on port 8080:
Tomcat started on port 8080 (http) with context path '/'
Started BookStoreApplication in 1.8 seconds
Verify with curl:
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.bookstoreand 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:
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
- Creates an
ApplicationContext. - Registers
@SpringBootApplicationas a configuration source. - Runs auto-configuration based on classpath (web starter →
DispatcherServlet). - Scans for
@Componentclasses under the main class package. - Starts the embedded web server.
- 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
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:
@SpringBootTest
class BookStoreApplicationTests {
@Test
void contextLoads() { }
}
Common first-run problems
Fixes that unblock beginners
- Port 8080 in use — Set
server.port=8081or kill the conflicting process. - Java version mismatch — Boot 3.x requires Java 17; run
java -versionto 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
./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.
@SpringBootApplicationenables 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.ymlexternalises port, logging, and application name../mvnw packageproduces a self-contained JAR for deployment.
Test yourself
Answer these before moving on — recall is what makes it stick.