Spring Boot Lab Setup and Reading Order
Spring Boot helps configure dependencies, apply auto-configuration, and package Spring applications. An executable jar with an embedded server offers an alternative to deploying a WAR into a separately managed server.
These chapters use small examples to teach bean injection, HTTP requests and responses, external API calls, SQL mapping, and configuration to readers who know basic Java. Create the shared project first, then add the files needed by each chapter.
Versions and prerequisites
Section titled “Versions and prerequisites”| Component | Baseline |
|---|---|
| Java | JDK 17 or later, source level 17 |
| Spring Boot | 3.5.16 |
| Maven | 3.6.3 or later |
| MyBatis Starter | Add 3.0.4 in the MyBatis chapter |
| Database | Use H2 in the MyBatis chapter |
Check the official system requirements for the Java compatibility range of Spring Boot 3.5. Keep example dependencies aligned with this baseline, and review support status and security updates for the version used in a real service.
Base Maven project
Section titled “Base Maven project”Create these two files in an empty directory. Each code block title specifies its path. Work in a separate copy of the base project for each chapter so its application.properties and initialization data do not overwrite those of another chapter.
<project xmlns="http://maven.apache.org/POM/4.0.0"12 collapsed lines
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>3.5.16</version> <relativePath/> </parent> <groupId>com.example</groupId> <artifactId>tutorial</artifactId> <version>1.0.0</version> <properties> <java.version>17</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-webflux</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build></project>spring-boot-starter-web provides Spring MVC and an embedded web server. This exercise includes spring-boot-starter-webflux for WebClient. With both starters present, the default web application type is Spring MVC. The test dependency provides JUnit and Spring testing tools.
package com.example.tutorial;
import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplicationpublic class TutorialApplication { public static void main(String[] args) { SpringApplication.run(TutorialApplication.class, args); }}Keep all example classes under com.example.tutorial so they fall within the default component scan of @SpringBootApplication. The MyBatis chapter’s mappers are discovered within the same boundary.
Run the commands
Section titled “Run the commands”Run these commands in sequence from the project root.
java -versionmvn -versionmvn testmvn packagejava -jar target/tutorial-1.0.0.jarmvn test runs the tests you add. The base project alone has no tests. mvn package runs tests and builds an executable jar. Start the server with the final command and stop it with Ctrl+C.
The base project has no HTTP routes yet, so a 404 response at / is expected. Add a controller from the REST API chapter before requesting its routes. During development, mvn spring-boot:run can start the application directly.
Reading order
Section titled “Reading order”- Beans and constructor injection: Select beans of one interface and check the returned values.
- REST API requests and JSON responses: Separate input locations, object serialization, status codes, and headers.
- External API calls with WebClient: Configure addresses and timeouts and connect asynchronous responses.
- MyBatis and H2: Use seed data and mappers to check inserts, queries, and deletes.
- Configuration and effective values: Apply profiles and external configuration to the same jar.
How to use the examples
Section titled “How to use the examples”Save a block whose title is a path to that file. Blocks labeled as fragments, such as pom.xml (dependencies), belong inside the corresponding part of the base file. Configuration and SQL under src/main/resources are packaged as classpath resources.
Expected responses and test code give readers a comparison target. Prepare external API addresses, secret injection, and production databases according to each chapter’s scope. Design authentication, authorization, and production deployment policies around the service’s own requirements.