Deine erste App: start.spring.io
Ein Projekt generieren, jeden Teil davon verstehen und BookVault zum ersten Mal auf einem eingebetteten Server starten.
Deutsche Übersetzung in Arbeit
Diese Lektion ist noch nicht ins Deutsche übersetzt und wird daher auf Englisch angezeigt. Der Rest der Seite ist vollständig lokalisiert.
Auf dieser Seite
Enough theory - let's create a running application. This is the birth of BookVault, the community-library API you'll grow across the whole Spring path.
Generate the project
The fastest way to start any Spring project is start.spring.io (the "Spring Initializr"). You pick your options and it hands you a ready-to-run zip. For BookVault:
- Project: Maven · Language: Java · Spring Boot: 4.x
- Group:
com.bookvault· Artifact:bookvault· Java: 21 - Dependencies: Spring Web
Your IDE has this built in
IntelliJ IDEA (New Project -> Spring) and VS Code (the Spring Initializr command) wrap the exact same service, so you never leave your editor. The finished BookVault lives in the companion repo linked from this path - clone it to compare with your own.
What it generated
Unzip it and you'll find a complete, runnable project. The build file declares the one dependency you chose (plus test), and the Boot parent that manages versions:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>4.0.0</version>
</parent>
<properties>
<java.version>21</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>The rest of the tree is small and predictable:
src/main/java/com/bookvault/BookVaultApplication.java # the @SpringBootApplication main class
src/main/resources/application.yml # configuration
src/test/java/com/bookvault/BookVaultApplicationTests.javaRun it
./mvnw spring-boot:run # Maven
./gradlew bootRun # GradleBoot starts an embedded Tomcat server - there's no separate server to install or deploy a WAR to. The app is the server:
:: Spring Boot :: (v4.0.0) INFO Starting BookVaultApplication using Java 21 INFO Tomcat initialized with port 8080 (http) INFO Tomcat started on port 8080 (http) with context path '/' INFO Started BookVaultApplication in 1.24 seconds
Add the first endpoint
A single controller makes BookVault answer HTTP. For now it returns a hard-coded catalog - we'll make it real in the Web and Data modules:
@RestController
@RequestMapping("/api/books")
class BookController {
record Book(String isbn, String title, String author) {}
@GetMapping
List<Book> all() {
return List.of(
new Book("978-0134685991", "Effective Java", "Joshua Bloch"),
new Book("978-1617294945", "Spring in Action", "Craig Walls")
);
}
}Restart, then:
curl http://localhost:8080/api/books[{"isbn":"978-0134685991","title":"Effective Java","author":"Joshua Bloch"},
{"isbn":"978-1617294945","title":"Spring in Action","author":"Craig Walls"}]You wrote a class and a method; Boot supplied the server, the HTTP handling, the JSON serialization, and the routing. That's auto-configuration earning its keep.
You returned a List<Book> (a Java record) from a method and the client received
JSON - you never wrote any serialization code. Name the two things Boot
auto-configured that made this happen, and which single dependency pulled them in.
How does a Spring Boot web app serve HTTP without installing a separate server?
Key takeaways
- start.spring.io (or your IDE) generates a complete, runnable Boot project from a few choices; BookVault uses Maven, Java 21, Boot 4.x.
- The generated project is tiny: a main class, application.yml, and a test - the Boot parent manages dependency versions.
- Boot embeds the web server (Tomcat by default), so the application is self-contained - no external server to deploy to.
- A @RestController method returning objects becomes a JSON HTTP endpoint automatically, thanks to auto-configured Spring MVC + Jackson.
- spring-boot-starter-web is the single dependency that brings web + JSON support.