In this section, we will guide you through the process of creating your first Spring Boot application. By the end of this lesson, you will have a basic Spring Boot application up and running. We will cover the following steps:
- Setting Up Your Project
- Understanding the Project Structure
- Writing Your First Controller
- Running Your Application
- Setting Up Your Project
Using Spring Initializr
Spring Initializr is a web-based tool that helps you bootstrap a new Spring Boot project. Follow these steps to set up your project:
- Navigate to Spring Initializr: Open your web browser and go to Spring Initializr.
- Project Metadata: Fill in the project metadata:
- Project: Maven Project
- Language: Java
- Spring Boot: Choose the latest stable version
- Project Metadata:
- Group:
com.example
- Artifact:
demo
- Name:
demo
- Description:
Demo project for Spring Boot
- Package name:
com.example.demo
- Packaging: Jar
- Java: 11 (or your preferred version)
- Group:
- Dependencies: Add the following dependencies:
- Spring Web
- Generate the Project: Click on the "Generate" button to download the project as a ZIP file.
- Extract the ZIP: Extract the downloaded ZIP file to a directory of your choice.
Importing the Project into Your IDE
- Open Your IDE: Open your preferred IDE (e.g., IntelliJ IDEA, Eclipse, or VS Code).
- Import the Project: Import the extracted project as a Maven project.
- In IntelliJ IDEA:
File -> Open
and select the project directory. - In Eclipse:
File -> Import -> Existing Maven Projects
and select the project directory.
- In IntelliJ IDEA:
- Understanding the Project Structure
Once the project is imported, you will see the following structure:
demo ├── src │ ├── main │ │ ├── java │ │ │ └── com │ │ │ └── example │ │ │ └── demo │ │ │ └── DemoApplication.java │ │ └── resources │ │ ├── application.properties │ │ └── static │ │ └── templates │ └── test │ └── java │ └── com │ └── example │ └── demo │ └── DemoApplicationTests.java ├── mvnw ├── mvnw.cmd ├── pom.xml └── README.md
Key Files and Directories
- DemoApplication.java: The main class that bootstraps the Spring Boot application.
- application.properties: Configuration file for the application.
- static: Directory for static resources (e.g., HTML, CSS, JavaScript).
- templates: Directory for server-side templates (e.g., Thymeleaf, FreeMarker).
- DemoApplicationTests.java: Class for writing unit tests.
- Writing Your First Controller
Let's create a simple REST controller that returns a "Hello, World!" message.
Step-by-Step Guide
- Create a New Controller Class: In the
com.example.demo
package, create a new Java class namedHelloController
.
package com.example.demo; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloController { @GetMapping("/hello") public String sayHello() { return "Hello, World!"; } }
Explanation
- @RestController: This annotation marks the class as a REST controller.
- @GetMapping("/hello"): This annotation maps HTTP GET requests to the
sayHello
method. - sayHello(): This method returns a simple "Hello, World!" message.
- Running Your Application
Using Your IDE
- Run the Application: In your IDE, locate the
DemoApplication
class and run it as a Java application.- In IntelliJ IDEA: Right-click on
DemoApplication
and selectRun 'DemoApplication'
. - In Eclipse: Right-click on
DemoApplication
and selectRun As -> Java Application
.
- In IntelliJ IDEA: Right-click on
Using the Command Line
- Navigate to the Project Directory: Open a terminal and navigate to the project directory.
- Build the Project: Run the following command to build the project:
- Run the Application: Run the following command to start the application:
Accessing the Application
- Open Your Web Browser: Open your web browser and go to
http://localhost:8080/hello
. - Verify the Output: You should see the message "Hello, World!".
Conclusion
Congratulations! You have successfully created and run your first Spring Boot application. In this lesson, you learned how to:
- Set up a new Spring Boot project using Spring Initializr.
- Understand the basic project structure.
- Create a simple REST controller.
- Run the application and verify the output.
In the next module, we will dive deeper into Spring Boot basics, including annotations, dependency injection, and configuration.
Spring Boot Course
Module 1: Introduction to Spring Boot
- What is Spring Boot?
- Setting Up Your Development Environment
- Creating Your First Spring Boot Application
- Understanding Spring Boot Project Structure
Module 2: Spring Boot Basics
- Spring Boot Annotations
- Dependency Injection in Spring Boot
- Spring Boot Configuration
- Spring Boot Properties
Module 3: Building RESTful Web Services
- Introduction to RESTful Web Services
- Creating REST Controllers
- Handling HTTP Methods
- Exception Handling in REST
Module 4: Data Access with Spring Boot
- Introduction to Spring Data JPA
- Configuring Data Sources
- Creating JPA Entities
- Using Spring Data Repositories
- Query Methods in Spring Data JPA
Module 5: Spring Boot Security
- Introduction to Spring Security
- Configuring Spring Security
- User Authentication and Authorization
- Implementing JWT Authentication
Module 6: Testing in Spring Boot
Module 7: Advanced Spring Boot Features
Module 8: Deploying Spring Boot Applications
Module 9: Performance and Monitoring
- Performance Tuning
- Monitoring with Spring Boot Actuator
- Using Prometheus and Grafana
- Logging and Log Management