Sub topic 2
Setting up a Spring Boot web project
This chapter creates the project that the rest of the tutorial builds on. It assumes you know Java 17 or later, Maven basics (Gradle works the same way), and the idea of dependency injection. By the end you will have an application that starts an embedded web server and is ready for controllers.
What a web project needs
A Spring Boot web application is a normal Java application with three additions:
- A parent or BOM (bill of materials) that pins compatible library versions.
- Starter dependencies, which are single dependencies that pull in a tested set of libraries.
- A main class annotated with
@SpringBootApplication.
You don't install a separate application server. The web starter includes Tomcat as a library and starts it from main.
Generating the project
The usual route is Spring Initializr at start.spring.io. Choose Maven, Java 17 or later, and the current stable Spring Boot 3.x release. Add the dependencies Spring Web and Validation. Download the zip and open it in your IDE. Writing the files by hand also works, and the next sections show what they contain.
The build file
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
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.4.1</version>
<relativePath/>
</parent>
<groupId>com.example</groupId>
<artifactId>books</artifactId>
<version>0.0.1-SNAPSHOT</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-validation</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>The starter parent manages dependency versions, so the dependencies above have no <version> element. Spring Boot 3.x needs Java 17 or later.
| Starter | What it provides |
|---|---|
| `spring-boot-starter-web` | Spring MVC, Jackson for JSON, embedded Tomcat |
| `spring-boot-starter-validation` | Bean Validation and the Hibernate Validator implementation |
| `spring-boot-starter-test` | JUnit 5, Mockito, MockMvc, AssertJ |
The main class
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 marks the class as a source of beans. @EnableAutoConfiguration configures Spring MVC, Jackson, and Tomcat because they are on the classpath. @ComponentScan scans the class's package and all sub-packages for components.
Configuration
Spring Boot reads src/main/resources/application.properties. One setting is useful now:
server.port=80808080 is already the default. Change it when another process uses that port.
Running and checking
./mvnw spring-boot:runIf the project has no Maven wrapper, run mvn spring-boot:run instead. The log should end with lines like Tomcat started on port 8080 and Started BooksApplication. Then call the server:
curl -i http://localhost:8080/anythingYou should get 404 with a small JSON error body. That is the expected result, because no controller exists yet. It shows that Tomcat is listening and MVC is handling requests.
Common setup errors
- Port already in use: another process holds 8080. Stop it or change
server.port. - Wrong Java version: a build error such as "release version 17 not supported" means your JDK is older than 17.
- Explicit versions on starters: they override the managed versions and can cause conflicts. Leave them out.
Exercise
You add a controller in the package com.example.web, but the main class is in com.example.books.app. Requests to the controller return 404. What is the cause, and what are two ways to fix it?
Show answer
Component scanning starts at the main class's package (com.example.books.app) and covers only its sub-packages. com.example.web is outside that tree, so the controller is never registered. Fix it by moving the main class to a common root package such as com.example, or by listing the extra package in @SpringBootApplication(scanBasePackages = "com.example").