Sub topic 6
Generating a project with Spring Initializr
This chapter assumes you have a JDK installed, a terminal, and a basic understanding of Maven or Gradle as a user. You should also know what a Spring Boot starter is from the earlier chapters. Here you will generate a project, read the choices on the form, and learn which choices cause trouble later.
What Spring Initializr is
Spring Initializr is a web service that generates the skeleton of a Spring Boot project. You choose a build tool, a language, a Boot version, some metadata, and a list of dependencies. It returns a zip archive that contains:
- a build file (
pom.xmlorbuild.gradle) with the Boot parent or plugin and your starters - the Maven or Gradle wrapper scripts
- a main class annotated with
@SpringBootApplication - an empty
application.propertiesfile - a test class that checks the application context starts
You could write all of this by hand. Initializr saves time and, more importantly, it picks dependency versions that were tested together for the Boot version you choose.
The same service sits behind several front ends:
| Front end | How you reach it |
|---|---|
| Web form | `https://start.spring.io` in a browser |
| HTTP API | `curl` against `https://start.spring.io/starter.zip` |
| IDE wizard | IntelliJ IDEA (Spring Boot project type), Spring Tools for Eclipse and VS Code, and others |
| Spring Boot CLI | `spring init` |
All of them send the same parameters to the same service, so the choices below apply to each one.
The fields on the form
The form has two groups of choices. The first group shapes the build. The second group is metadata that names the project.
Build choices
- Project: Maven or Gradle (Groovy DSL or Kotlin DSL). This decides which build file you get. Pick the tool your team already uses. The rest of this tutorial shows Maven.
- Language: Java, Kotlin, or Groovy. Choose Java here.
- Spring Boot version: Initializr preselects the latest stable release. Versions marked
SNAPSHOT,M1,RC1and similar are unreleased builds. Avoid them unless you are testing a preview on purpose. - Packaging: Jar or War. Choose Jar. A Boot application embeds its own server, so a Jar runs with
java -jar. War is only for deploying into an external servlet container, and it adds extra files to the project. - Java version: the language level the build compiles for. Boot 3.x and later need Java 17 or newer. Pick a version that is equal to or lower than the JDK you have installed.
Metadata
| Field | Meaning | Example |
|---|---|---|
| Group | Reverse domain name of your organization. It becomes the Maven `groupId`. | `com.example` |
| Artifact | The project identifier. It becomes the `artifactId` and the JAR file name. | `demo` |
| Name | Display name. It defaults to the artifact and also names the main class (`DemoApplication`). | `demo` |
| Description | Free text stored in the build file. | `Demo project for Spring Boot` |
| Package name | Root Java package. It defaults to group plus artifact. | `com.example.demo` |
The package name matters more than it looks. @SpringBootApplication scans the package of the main class and everything below it. Your controllers, services, and repositories must live in that package or a sub-package. If you move the main class or create classes in a sibling package, the scan will not find them.
Choosing dependencies
Click Add Dependencies and search by name. Each entry you pick becomes a starter or another library in the build file. Some common ones and their IDs:
| Name on the form | ID for the API | What you get |
|---|---|---|
| Spring Web | `web` | Spring MVC, JSON support, embedded Tomcat |
| Spring Boot DevTools | `devtools` | Automatic restart during development |
| Spring Boot Actuator | `actuator` | Health and metrics endpoints |
| Spring Data JPA | `data-jpa` | Database access with JPA and Hibernate |
| H2 Database | `h2` | In-memory database for experiments |
For the project in this tutorial, add only Spring Web. You can add more later by editing the build file, so you do not need to guess everything now. Do add the dependencies you know you need, though. Adding a starter later is easy. Removing a stray one that triggers unwanted auto-configuration can be confusing.
A frequent mistake is to add Spring Data JPA without a database driver. Boot then tries to configure a DataSource and fails at startup with a message about being unable to determine a database driver class. Pair JPA with a driver such as H2 or PostgreSQL.
The Explore button shows the generated files in the browser before you download them. It is a quick way to see exactly what your choices produce. The Share button creates a URL that stores the current form state, which is useful for passing a configuration to a teammate.
Generating from the command line
The web form is one client of an HTTP API. Because the API takes plain form parameters, you can script project creation. This command produces the same project as the form:
curl https://start.spring.io/starter.zip \
-d type=maven-project \
-d language=java \
-d javaVersion=17 \
-d packaging=jar \
-d groupId=com.example \
-d artifactId=demo \
-d name=demo \
-d description="Demo project for Spring Boot" \
-d packageName=com.example.demo \
-d dependencies=web \
-d baseDir=demo \
-o demo.zip
unzip demo.zip
cd demo
./mvnw --versionNotes on the parameters:
-dsends each value in a POST form body. Quote any value that contains spaces.- If you omit
bootVersion, the service uses its current default release. Set it explicitly (for example-d bootVersion=3.3.5) when you need a reproducible result. Use a version the service lists as available. dependenciestakes comma-separated IDs, such asweb,devtools,actuator.baseDirputs everything inside a folder of that name. Without it, the archive contents land directly in your current directory when unzipped, which can mix them with other files.- Use
type=gradle-projectfor Gradle with Groovy, ortype=gradle-project-kotlinfor the Kotlin DSL.
To see every option and ID the service currently supports, request its metadata:
curl -H "Accept: application/json" https://start.spring.ioOn a machine with the Spring Boot CLI installed, spring init --dependencies=web demo does the same job.
What to check after unzipping
Open the project and confirm four things before you write any code.
- The build file declares the Boot parent (Maven) or plugin (Gradle), and the starters you chose appear without version numbers.
- The main class sits at the root of your package, for example
src/main/java/com/example/demo/DemoApplication.java. mvnwandmvnw.cmd(orgradlew) are present. On macOS and Linux, check thatmvnwis executable. If it is not, runchmod +x mvnw../mvnw testsucceeds. The generated test,contextLoads, starts the application context. A pass means your JDK, wrapper, and dependencies work together.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| `invalid target release: 21` or a similar compiler error | The selected Java version is newer than the installed JDK | Run `java -version`, then change `java.version` in `pom.xml` or regenerate with a matching version |
| Classes in a new package are not picked up | They are outside the main class's package tree | Move them under the root package, or move the main class up |
| `Permission denied` on `./mvnw` | The execute bit was lost when unzipping | `chmod +x mvnw` |
| Unzipped files are scattered across the current folder | The archive had no top-level folder | Delete them, and unzip into a new directory or use `baseDir` |
| Project builds but `/hello` returns 404 | Spring Web was not selected, so no MVC or server is present | Add `spring-boot-starter-web` to the build file |
| Package name looks odd, such as `com.example.myapp` for artifact `my-app` | Initializr removes characters that are invalid in package names | Edit the package name field before generating |
Exercise
You generate a project with group org.acme and artifact order-service, then add a @RestController class in the package org.acme.web. The main class is in org.acme.orderservice. The endpoint returns 404. Why, and how do you fix it?
Show answer
Component scanning starts at the package of the @SpringBootApplication class and covers only that package and its sub-packages. org.acme.web is a sibling of org.acme.orderservice, so the controller is never registered as a bean. Move the controller into org.acme.orderservice (or a sub-package such as org.acme.orderservice.web), or move the main class up to org.acme.
Exercise
Write the curl options you would add to the generation command to get a Gradle (Kotlin DSL) project that includes Spring Web and Actuator.
Show answer
Use -d type=gradle-project-kotlin and -d dependencies=web,actuator. The other parameters (groupId, artifactId, javaVersion, and so on) stay the same.