Sub topic 2
Creating the project and choosing starters
This chapter covers generating the Spring Boot project and picking the dependencies a CRUD API needs. It assumes you know Java basics (classes, records, interfaces, annotations), have used Maven or Gradle, and understand HTTP methods, status codes, and JSON. You also need a working JDK (17 or later for Spring Boot 3) installed.
What a starter is
A starter is a single dependency that pulls in a curated set of libraries that work well together. You declare what you want to build, and the Spring Boot parent POM supplies compatible versions. That is why the dependencies below have no <version> element.
Spring Boot also uses auto-configuration. When it finds a library on the classpath, it configures sensible defaults for it. Adding spring-boot-starter-web gives you an embedded Tomcat server and JSON support without any setup code.
Generating the project
Spring Initializr at start.spring.io builds a project skeleton for you. Choose these settings:
- Project: Maven (Gradle works the same way).
- Language: Java.
- Spring Boot: the latest stable release, not a snapshot or milestone.
- Packaging: Jar, and a Java version your JDK supports.
- Dependencies: Spring Web, Spring Data JPA, Validation, H2 Database.
Download the zip, unpack it, and open it in your IDE. The generated project contains a pom.xml, a main class annotated with @SpringBootApplication, an empty application.properties, and one starter test. Spring Initializr also adds the test starter for you.
The dependencies for this API
The pom.xml <dependencies> section looks like this:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>Each one has a distinct job:
| Dependency | What it provides | Used in this tutorial for |
|---|---|---|
| `spring-boot-starter-web` | Spring MVC, embedded Tomcat, Jackson for JSON | Controllers and request mapping |
| `spring-boot-starter-data-jpa` | Spring Data JPA, Hibernate, transaction support | Entities and repositories |
| `spring-boot-starter-validation` | Bean Validation (Hibernate Validator) | Checking request bodies |
| `h2` | An in-memory SQL database | Development without installing a database |
| `spring-boot-starter-test` | JUnit, Mockito, MockMvc, AssertJ | Web layer tests |
The runtime scope on H2 means your code never compiles against it. The test scope on the test starter keeps testing libraries out of the packaged application.
How the layers connect
The dependencies map directly to the layers you will build in later chapters.
Running the empty application
Start the application from the project root:
./mvnw spring-boot:runWith H2 on the classpath and no datasource properties set, Spring Boot starts an embedded in-memory database automatically. The log ends with a line saying Tomcat started on port 8080. Nothing is exposed yet, so any request returns a 404 response. That is expected.
Common errors
- Dependencies not resolving. A
<version>is only optional when the project inherits fromspring-boot-starter-parent. If you removed the parent, add the Spring Boot BOM (spring-boot-dependencies) independencyManagement. jakartaimports failing. Spring Boot 3 usesjakarta.*packages. Older tutorials usingjavax.persistenceorjavax.validationwill not compile.- Port 8080 already in use. Stop the other process, or set
server.port=8081inapplication.properties. - Validation annotations ignored or missing. Since Spring Boot 2.3, validation is not included in the web starter. Without
spring-boot-starter-validation,@NotBlankand@Validhave no effect or fail to compile.
Exercise
You add spring-boot-starter-data-jpa and h2 to a new project but do not set any spring.datasource.* properties. What does Spring Boot do at startup, and what happens to saved data when the application stops?
Show answer
Auto-configuration detects H2 on the classpath and creates an embedded in-memory DataSource. The application starts without any connection settings. All saved data is lost when the application stops, because H2 keeps it in memory only.
Exercise
A teammate copies a validated DTO from an older tutorial and gets compile errors on import javax.validation.constraints.NotBlank. What are two likely causes?
Show answer
Spring Boot 3 moved to the jakarta.* namespace, so the import should be jakarta.validation.constraints.NotBlank. Also check that spring-boot-starter-validation is in the pom.xml, since the web starter no longer includes it.