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:

  1. Setting Up Your Project
  2. Understanding the Project Structure
  3. Writing Your First Controller
  4. Running Your Application

  1. 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:

  1. Navigate to Spring Initializr: Open your web browser and go to Spring Initializr.
  2. 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)
  3. Dependencies: Add the following dependencies:
    • Spring Web
  4. Generate the Project: Click on the "Generate" button to download the project as a ZIP file.
  5. Extract the ZIP: Extract the downloaded ZIP file to a directory of your choice.

Importing the Project into Your IDE

  1. Open Your IDE: Open your preferred IDE (e.g., IntelliJ IDEA, Eclipse, or VS Code).
  2. 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.

  1. 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.

  1. Writing Your First Controller

Let's create a simple REST controller that returns a "Hello, World!" message.

Step-by-Step Guide

  1. Create a New Controller Class: In the com.example.demo package, create a new Java class named HelloController.
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.

  1. Running Your Application

Using Your IDE

  1. 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 select Run 'DemoApplication'.
    • In Eclipse: Right-click on DemoApplication and select Run As -> Java Application.

Using the Command Line

  1. Navigate to the Project Directory: Open a terminal and navigate to the project directory.
  2. Build the Project: Run the following command to build the project:
./mvnw clean install
  1. Run the Application: Run the following command to start the application:
./mvnw spring-boot:run

Accessing the Application

  1. Open Your Web Browser: Open your web browser and go to http://localhost:8080/hello.
  2. 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

Module 2: Spring Boot Basics

Module 3: Building RESTful Web Services

Module 4: Data Access with Spring Boot

Module 5: Spring Boot Security

Module 6: Testing in Spring Boot

Module 7: Advanced Spring Boot Features

Module 8: Deploying Spring Boot Applications

Module 9: Performance and Monitoring

Module 10: Best Practices and Tips

© Copyright 2024. All rights reserved