Module: Getting Started

Creating Your First Spring Boot Project

Creating Your First Spring Boot Project

Welcome to the exciting world of Spring Boot! This section will guide you through creating your very first Spring Boot project. We'll use Spring Initializr, the easiest way to bootstrap a Spring Boot application.

What is Spring Initializr?

Spring Initializr (https://start.spring.io/) is a web-based tool that generates a basic Spring Boot project structure with pre-configured dependencies. It eliminates the boilerplate code you'd normally have to write, allowing you to focus on building your application logic.

Steps to Create Your Project

  1. Access Spring Initializr: Open your web browser and navigate to https://start.spring.io/.

  2. Project Metadata: You'll see a form with several options. Let's configure them:

    • Project: Select Maven Project or Gradle Project. For this tutorial, we'll use Maven Project. Gradle is also a popular choice, but Maven is often easier for beginners.
    • Language: Choose Java.
    • Spring Boot: Select the latest stable version. Avoid milestone or snapshot releases for learning. (As of writing, this is likely 3.2.x).
    • Project Metadata:
      • Group: Typically your organization's reverse domain name (e.g., com.example). Use com.example for now.
      • Artifact: The name of your project (e.g., my-first-spring-boot-app).
      • Name: A more descriptive name (e.g., My First Spring Boot App).
      • Description: A brief description of your project.
      • Package name: Automatically generated based on Group and Artifact. You can customize it if needed.
      • Packaging: Select Jar. This creates an executable JAR file.
      • Java: Select the Java version you have installed (e.g., 17, 21). Ensure your IDE and environment are configured to use the same version.
  3. Dependencies: This is where you add the libraries your project needs. For this first project, let's add just one dependency:

    • Click the "Add Dependencies..." button.
    • Search for and select Spring Web. This dependency provides the necessary components for building web applications, including REST controllers.
  4. Generate the Project: Click the "Generate" button at the bottom of the page. This will download a ZIP file containing your project's initial structure.

Importing the Project into Your IDE

  1. Extract the ZIP file: Unzip the downloaded file to a directory of your choice.

  2. Import as Maven Project: Open your IDE (IntelliJ IDEA, Eclipse, VS Code with Maven extension, etc.). Use the "Import Maven Project" (or equivalent) option. Navigate to the directory where you extracted the ZIP file and select the pom.xml file.

    • IntelliJ IDEA: File -> Open -> Select the pom.xml file
    • Eclipse: File -> Import -> Maven -> Existing Maven Projects -> Browse to the project directory -> Finish
    • VS Code: Open the folder containing the pom.xml file. VS Code will likely prompt you to import the Maven project.
  3. Wait for Dependencies to Download: Your IDE will now download the necessary dependencies defined in the pom.xml file. This may take a few minutes depending on your internet connection.

Project Structure Overview

Once the project is imported, you'll see a typical Maven project structure. Here are some key files and folders:

  • src/main/java: This is where your Java source code will reside.
  • src/main/resources: This folder contains resources like application properties, templates, and static content.
  • src/test/java: This is where your unit and integration tests will go.
  • pom.xml: The Maven Project Object Model (POM) file. It defines your project's dependencies, build configuration, and other metadata.
  • MyFirstSpringBootAppApplication.java: This is the main application class. It's the entry point for your Spring Boot application.

Running Your Application

  1. Locate the Main Application Class: Open the MyFirstSpringBootAppApplication.java file in your IDE.

  2. Run the Application: Right-click on the class and select "Run 'MyFirstSpringBootAppApplication'". Alternatively, you can run it from the command line using mvn spring-boot:run.

  3. Verify the Application is Running: You should see output in the console indicating that your Spring Boot application has started successfully. Look for a message like:

    Started MyFirstSpringBootAppApplication in ... seconds (JVM running on ...)
    

Next Steps

Congratulations! You've successfully created and run your first Spring Boot application. Now you're ready to start building more complex features. The next step is to create a simple REST controller to handle HTTP requests. We'll cover that in the next section.