Sub topic 2
Creating the project and choosing starters
This chapter covers how to generate the project, what each starter adds, and how to check that the skeleton runs before you write any code. It assumes you can read a Maven pom.xml, run a command in a terminal, and have JDK 17 or later installed. Gradle works the same way. The examples use Maven because the dependency list in this tutorial is written in Maven XML.
What Spring Initializr does
Spring Initializr is a web service at start.spring.io that generates a build file and a minimal source tree. Most IDEs call the same service from their "new Spring project" wizard. The generated project is ordinary Maven. You can edit it by hand afterwards, so none of the choices below are permanent.
You make six choices:
| Choice | What to pick for this tutorial | Notes |
|---|---|---|
| Project | Maven | Gradle is equally supported. |
| Language | Java | Kotlin and Groovy are also offered. |
| Spring Boot version | The default stable release | Avoid versions marked SNAPSHOT or M1, RC1 and so on. |
| Group and Artifact | `com.example` and `books` | These become the Maven coordinates and the base Java package. |
| Packaging | Jar | An embedded server is inside the jar, so no external Tomcat is needed. |
| Java version | 17 or 21 | Spring Boot 3.x requires at least Java 17. |
You can generate the project in the browser, or from a terminal with a single request. The terminal form is repeatable and easy to put in a README:
curl https://start.spring.io/starter.zip \
-d type=maven-project \
-d language=java \
-d groupId=com.example \
-d artifactId=books \
-d name=books \
-d packageName=com.example.books \
-d javaVersion=21 \
-d dependencies=web,data-jpa,validation,h2 \
-o books.zip
unzip books.zip -d books
cd booksLeaving out bootVersion makes the service use its current default. The dependencies parameter takes Initializr's short identifiers, and these are not the same as Maven artifact IDs. For example, web becomes spring-boot-starter-web.
What the generated project contains
books/
├── mvnw, mvnw.cmd, .mvn/
├── pom.xml
└── src/
├── main/java/com/example/books/BooksApplication.java
├── main/resources/application.properties
└── test/java/com/example/books/BooksApplicationTests.javaThe files mvnw and mvnw.cmd are the Maven Wrapper. They download the correct Maven version on first use, so everyone on the team builds with the same tool. Use ./mvnw instead of a locally installed mvn.
The application class is the entry point:
package com.example.books;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class BooksApplication {
public static void main(String[] args) {
SpringApplication.run(BooksApplication.class, args);
}
}@SpringBootApplication combines three annotations: @Configuration, @EnableAutoConfiguration and @ComponentScan. The last one matters most for project layout. It scans the package of this class and every package below it. Controllers, services and repositories placed outside com.example.books are never found, and the application then starts without them and returns 404 for their URLs.
The test class generated for you contains one test, contextLoads, annotated with @SpringBootTest. It starts the whole application context. It is a useful smoke test, but it will fail once a database is configured and unreachable. Later chapters show how to keep it from becoming a nuisance.
The parent and the starters
Open pom.xml. The important part is the parent:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.4.1</version>
<relativePath/>
</parent>
<properties>
<java.version>21</java.version>
</properties>The parent supplies a dependency-management section. It pins tested, compatible versions for hundreds of libraries, which is why none of the dependencies below carry a <version> element. It also configures the compiler plugin and the packaging plugin. If you add a library that Boot manages and give it your own version, you override the tested one, and that is a common source of mismatches that are hard to trace.
A starter is a dependency with no code of its own. It is a curated list of other dependencies that work together. This is the dependency block for the books API:
<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>The generator adds spring-boot-starter-test automatically. The other four come from your selections.
| Dependency | Main contents | Used in this tutorial for |
|---|---|---|
| `spring-boot-starter-web` | Spring MVC, Jackson (JSON), embedded Tomcat | Controllers, request mapping, JSON conversion |
| `spring-boot-starter-data-jpa` | Spring Data JPA, Hibernate, connection pool (HikariCP), transaction support | Entities, repositories, `@Transactional` |
| `spring-boot-starter-validation` | Jakarta Bean Validation API, Hibernate Validator | `@NotBlank` and `@Valid` on request bodies |
| `h2` (runtime) | In-memory SQL database | Development without installing a database |
| `spring-boot-starter-test` (test) | JUnit 5, Mockito, AssertJ, MockMvc, Spring Test | Web-layer tests |
The diagram shows how one starter fans out into libraries that you would otherwise pick and align yourself.
Scopes matter
Maven scope decides when a jar is on the classpath.
- The default scope,
compile, puts the jar on every classpath. The three Spring starters use it. runtimemeans the jar is not needed to compile your code, only to run it. A JDBC driver fits this, because your code never references driver classes directly.testkeeps the jar out of the packaged application. If you leave test libraries in compile scope, they ship in production.
Why H2 now and PostgreSQL later
H2 lets the application start with no database server. When Spring Boot sees H2 on the classpath and no datasource URL in the properties, it auto-configures an in-memory database. Because it is embedded, Boot also defaults spring.jpa.hibernate.ddl-auto to create-drop, which means Hibernate creates the tables at startup and drops them at shutdown. That default is convenient for experiments. It is not appropriate for real data, and the next chapter switches to a PostgreSQL URL and validate.
Moving to PostgreSQL also needs its driver, org.postgresql:postgresql, with runtime scope. If you set a PostgreSQL URL without the driver, startup fails with a message that no suitable driver or datasource class can be determined.
Running the empty skeleton
Start the application:
./mvnw spring-boot:runThe log should end with lines saying that Tomcat started on port 8080 and that BooksApplication started. Then request a URL that does not exist yet:
curl -i http://localhost:8080/api/booksYou should get a 404 with a JSON error body. That response is the success signal here. It proves that the web server, the JSON converter and the error handling work, and that no controller exists yet. Stop the application with Ctrl+C.
Common errors at this stage
- "release version 21 not supported" or "invalid target release". The
java.versionproperty is higher than the JDK running Maven. Check with./mvnw -vand either install the right JDK or lower the property. package jakarta.persistence does not exist. The JPA starter is missing from thepom.xml, or an IDE has not reloaded the Maven project. Spring Boot 3 usesjakarta.*packages, notjavax.*. Old tutorials withjavax.persistenceimports will not compile.@Validor@NotBlanknot found, or validation silently skipped. Since Spring Boot 2.3,spring-boot-starter-webno longer includes validation. You needspring-boot-starter-validation.- Port 8080 already in use. Another process holds the port. Set
server.port=8081inapplication.properties, or stop the other process. - Controllers ignored. The class sits outside the package of
BooksApplication. Move it undercom.example.books.
Keeping the dependency list small
Each starter adds startup time, memory and attack surface, so add only what you use. Initializr offers many options, such as Actuator and DevTools. Both are good additions for real services, and neither is needed to follow this tutorial. You can add any starter later by pasting its <dependency> block into the pom.xml. You do not have to regenerate the project.
Exercise
You add a controller class in the package com.example.controllers, while the application class is com.example.books.BooksApplication. The application starts without errors, but every request to the controller's URL returns 404. What is the cause, and give two ways to fix it?
Show answer
@SpringBootApplication scans only com.example.books and its subpackages, so the controller in com.example.controllers is never registered as a bean. Fix it by moving the controller into a subpackage of com.example.books, such as com.example.books.web. Alternatively, set the scan explicitly with @SpringBootApplication(scanBasePackages = {"com.example.books", "com.example.controllers"}). Moving the class is the simpler choice.
Exercise
Why does the H2 dependency use runtime scope and spring-boot-starter-test use test scope, instead of the default scope for both?
Show answer
H2 is needed only when the application runs, because your code never calls its classes directly, so runtime keeps it off the compile classpath. The test starter holds JUnit, Mockito and MockMvc, which only tests use, so test scope keeps them out of the packaged application.