Chapter 1: Spring Initializr
Introduction
Spring Initializr is a web project generator that allows you to quickly create a Spring Boot project with the necessary dependencies and basic configuration. It is the recommended starting point for any new Spring Boot project.
Chapter Objectives
By the end of this chapter, you will be able to:
- Use Spring Initializr to create Spring Boot projects
- Understand the different configuration options
- Choose the right dependencies for your project
- Generate and import a project into your IDE
What is Spring Initializr?
Definition
Spring Initializr is a bootstrap tool that generates the basic structure of a Spring Boot project. It:
- Creates the Maven or Gradle directory structure
- Generates the
pom.xmlorbuild.gradlefile with dependencies - Adds the main class with
@SpringBootApplication - Configures the basic resource files
Accessing Spring Initializr
There are several ways to access Spring Initializr:
- Web Interface: https://start.spring.io/
- Integrated IDE: IntelliJ IDEA, VS Code with Spring extensions
- Spring CLI: Command line
- REST API: For automation
Spring Initializr Web Interface
Access URL
https://start.spring.io/
Main Sections
The interface is divided into several sections:
Project Configuration
1. Project Type
Available Choices:
| Type | Description | Usage |
|---|---|---|
| Maven | XML build tool | Recommended for beginners, well-documented |
| Gradle | Groovy/Kotlin DSL build tool | More modern, faster, concise syntax |
Recommendation: Maven for this course (more standard in the Java industry)
2. Language
Available Choices:
| Language | Description | Level |
|---|---|---|
| Java | Main language of Spring | Recommended |
| Kotlin | Modern language by JetBrains | Advanced |
| Groovy | Dynamic language | Less common |
Recommendation: Java (language of this course)
3. Spring Boot Version
Version format: X.Y.Z
- X: Major version (major changes)
- Y: Minor version (new features)
- Z: Patch version (bug fixes)
Example:
3.2.0
Version types:
| Type | Suffix | Meaning | Usage |
|---|---|---|---|
| Release | None | Stable version | Production |
| Snapshot | -SNAPSHOT | Development version | Development only |
| Milestone | -M1, -M2 | Beta version | Testing |
| Release Candidate | -RC1 | Release candidate | Final testing |
Recommendation: Always choose the latest stable version (no suffix)
Project Metadata
Metadata Structure
1. Group
Definition: Organization or team identifier (Maven convention)
Format: Reversed domain name
Examples:
com.example # Generic example
com.inskillflow # Organization inskillflow
org.springframework # Spring Framework
Convention: Use your reversed domain or com.example for exercises
2. Artifact
Definition: Unique project identifier
Format: Lowercase letters, hyphens for spaces
Examples:
greeting-app
user-management-service
e-commerce-backend
Convention: Descriptive name in kebab-case
3. Name
Definition: Human-readable project name
Format: Human-readable name
Examples:
Greeting Application
User Management Service
E-Commerce Backend
4. Description
Definition: Short project description
Example:
Spring Boot application to manage greetings
5. Package Name
Definition: Root Java package of the project
Format: Automatically generated: {group}.\{artifact\}
Example:
com.example.greeting
Resulting structure:
src/main/java/com/example/greeting/
├── GreetingApplication.java
├── controller/
├── service/
└── model/
6. Packaging
Available Choices:
| Type | Description | Usage |
|---|---|---|
| JAR | Java Archive (standalone) | Spring Boot applications with embedded server |
| WAR | Web Archive | Deployment on external server (Tomcat, etc.) |
Recommendation: JAR (Spring Boot includes its own server)
Difference:
7. Java Version
Versions supported by Spring Boot 3.x:
| Version | Support | Recommendation |
|---|---|---|
| Java 17 | LTS | Recommended |
| Java 21 | LTS | Very recent |
| Java 11 | Old LTS | Not supported by Spring Boot 3.x |
Important Note: Spring Boot 3.x requires minimum Java 17
Dependency Management
What is a Dependency?
A dependency is an external library that your project uses.
Spring Boot Starters
Spring Boot uses the concept of "starters" - bundled dependencies.
Format of a starter:
spring-boot-starter-{technology}
Dependency Categories
1. Web
| Starter | Description | Usage |
|---|---|---|
| Spring Web | REST APIs, Spring MVC | REST web applications |
| Spring Reactive Web | WebFlux (reactive) | Highly scalable applications |
2. SQL (Relational Databases)
| Starter | Description |
|---|---|
| Spring Data JPA | ORM with Hibernate |
| JDBC API | Low-level SQL access |
| MyBatis Framework | SQL/Java mapping |
3. NoSQL
| Starter | Description |
|---|---|
| Spring Data MongoDB | Document database |
| Spring Data Redis | In-memory cache |
| Spring Data Elasticsearch | Full-text search |
4. Security
| Starter | Description |
|---|---|
| Spring Security | Authentication and authorization |
| OAuth2 Client | Social login (Google, Facebook) |
| OAuth2 Resource Server | Protected API |
5. SQL Drivers
| Driver | Database |
|---|---|
| PostgreSQL Driver | PostgreSQL |
| MySQL Driver | MySQL/MariaDB |
| H2 Database | In-memory database (tests) |
| Oracle Driver | Oracle Database |
6. I/O
| Starter | Description |
|---|---|
| Validation | Data validation (@Valid) |
| Spring Web Services | SOAP Web Services |
| Spring for Apache Kafka | Kafka Messaging |
7. Developer Tools
| Tool | Description | Usage |
|---|---|---|
| Spring Boot DevTools | Auto-reloading | Development |
| Lombok | Reduces boilerplate code | Productivity |
| Spring Configuration Processor | Config autocompletion | Development |
Dependencies for a Simple REST Project
For a basic REST application, choose:
✅ Spring Web
✅ Spring Boot DevTools
Dependencies for a Complete Application
For an application with a database:
✅ Spring Web
✅ Spring Data JPA
✅ PostgreSQL Driver (or other)
✅ Validation
✅ Spring Boot DevTools
✅ Lombok (optional)
Generation and Download
Generation Options
Available Buttons:
-
EXPLORE (Ctrl + Space)
- Preview project structure
- View
pom.xmlcontent - Check generated files
-
SHARE
- Generate a shareable link
- Share configuration with the team
-
GENERATE (Ctrl + Enter)
- Download project as ZIP
- Ready to be unzipped and imported
Structure of the Generated ZIP
greeting-app.zip
└── greeting/
├── .mvn/ # Maven Wrapper
├── src/
│ ├── main/
│ │ ├── java/
│ │ │ └── com/example/greeting/
│ │ │ └── GreetingApplication.java
│ │ └── resources/
│ │ ├── static/ # Static files (CSS, JS, images)
│ │ ├── templates/ # Templates (Thymeleaf)
│ │ └── application.properties
│ └── test/
│ └── java/
│ └── com/example/greeting/
│ └── GreetingApplicationTests.java
├── .gitignore
├── mvnw # Maven wrapper (Unix)
├── mvnw.cmd # Maven wrapper (Windows)
├── pom.xml # Maven configuration
└── HELP.md
Import into an IDE
Option 1: Visual Studio Code
Steps:
- Unzip the ZIP
- VS Code → File → Open Folder
- Select the
greetingfolder - Wait for automatic Maven import
- Required extensions:
- Extension Pack for Java
- Spring Boot Extension Pack
Option 2: IntelliJ IDEA
Steps:
- Unzip the ZIP
- IntelliJ → File → Open
- Select the
greetingfolder - IntelliJ automatically detects the Maven project
- Wait for indexing
Option 3: Eclipse/Spring Tool Suite (STS)
Steps:
- Unzip the ZIP
- Eclipse → File → Import
- Maven → Existing Maven Projects
- Select the
greetingfolder - Finish
Alternatives to the Web Interface
1. Spring Initializr in VS Code
Installation:
- Extension: Spring Initializr Java Support
Usage:
- Ctrl + Shift + P
- Type: "Spring Initializr: Create a Maven Project"
- Follow the prompts
2. Spring CLI
Installation:
# macOS
brew install spring-boot
# Linux/Windows
# Download from: https://docs.spring.io/spring-boot/docs/current/reference/html/getting-started.html#getting-started-installing-the-cli
Usage:
spring init \
--dependencies=web,data-jpa,postgresql \
--name=greeting \
--group-id=com.example \
--artifact-id=greeting \
greeting-app
3. Spring Initializr REST API
Base URL:
https://start.spring.io
Example with curl:
curl https://start.spring.io/starter.zip \
-d dependencies=web,data-jpa \
-d name=greeting \
-d groupId=com.example \
-d artifactId=greeting \
-o greeting.zip
Practical Exercises
Exercise 1: Create a Simple Project
Objective: Create a simple REST project
Instructions:
- Go to https://start.spring.io/
- Configure:
- Type: Maven
- Java: 17
- Group:
com.example - Artifact:
hello-rest
- Add: Spring Web
- Generate and download
- Import into VS Code
Exercise 2: Create a Project with Database
Objective: Create a project with JPA
Instructions:
- Go to https://start.spring.io/
- Configure:
- Type: Maven
- Java: 17
- Group:
com.example - Artifact:
user-manager
- Add:
- Spring Web
- Spring Data JPA
- PostgreSQL Driver
- Validation
- Generate and explore the
pom.xml
Exercise 3: Analyze a pom.xml
Objective: Understand the generated dependencies
Instructions:
- Generate a project with Spring Web
- Open the
pom.xml - Identify:
- The Spring Boot parent
- The web starter
- The Spring Boot Maven plugin
- List all transitive dependencies
Review Questions
-
What is Spring Initializr?
-
What is the difference between Maven and Gradle?
-
What does "starter" mean in Spring Boot?
-
What is the convention for the Group ID?
-
Why choose JAR over WAR for Spring Boot?
-
What is the minimum Java version for Spring Boot 3.x?
-
What is Spring Boot DevTools used for?
-
Which dependency is needed to create REST APIs?
Chapter Summary
Key Points
- Spring Initializr quickly generates Spring Boot projects
- Accessible via web, IDE, CLI, or API
- Configuration in 3 sections: Project, Metadata, Dependencies
- "Starters" group coherent dependencies
- JAR is the recommended packaging for Spring Boot
- Java 17+ is required for Spring Boot 3.x
Best Practices
- Use stable versions of Spring Boot
- Choose only necessary dependencies
- Follow Maven naming conventions
- Use DevTools in development
- Version code from creation
Next Steps
In the next chapter, we will explore in detail:
Chapter 2: Structure of a Spring Boot Project
- Package organization
- Role of each file
- Naming conventions
- Layered architecture