How to Create a Spring Boot Project in IntelliJ IDEA

In this tutorial, we will walk through the process of creating a Spring Boot project using IntelliJ IDEA. We will cover setting up IntelliJ IDEA, creating a new Spring Boot project using Spring Initializr, and running your first Spring Boot application.

Prerequisites

Before we start, ensure you have the following:

  • Java Development Kit (JDK) installed
  • IntelliJ IDEA installed (Community or Ultimate Edition)

Step 1: Setting Up IntelliJ IDEA

IntelliJ IDEA is a popular IDE for Java development that provides excellent support for Spring Boot applications. Follow these steps to set up IntelliJ IDEA:

  1. Download and Install IntelliJ IDEA:

    • Download IntelliJ IDEA from the JetBrains website.
    • Follow the installation instructions for your operating system.
  2. Launch IntelliJ IDEA:

    • Open IntelliJ IDEA from your applications menu.

Step 2: Creating a New Spring Boot Project

  1. Open New Project Wizard:

    • In the IntelliJ IDEA Welcome screen, click on New Project.
  2. Select Project Type:

    • In the New Project dialog, select Spring Initializr on the left side.
  3. Configure Project Metadata:

    • Service URL: Keep the default (https://start.spring.io).
    • Name: Enter the name of your project (e.g., spring-boot-demo).
    • Group: Enter your group ID (e.g., com.example).
    • Artifact: Enter your artifact ID (e.g., spring-boot-demo).
    • Type: Choose Maven Project.
    • Packaging: Choose Jar.
    • Java Version: Ensure you select the appropriate Java version (e.g., 17 if you are using JDK 17).
    • Click Next.
  4. Select Dependencies:

    • On the Dependencies screen, select the dependencies you need. For a basic Spring Boot application, you can start with:
      • Spring Web
    • Click Next.
  5. Review and Finish:

    • Review your project details and click Finish.

IntelliJ IDEA will create a new Spring Boot project with the specified configurations and dependencies.

Step 3: Exploring the Project Structure

After creating the project, you will see the following structure in your IntelliJ IDEA Project Explorer:

spring-boot-demo
├── src
│   ├── main
│   │   ├── java
│   │   │   └── com
│   │   │       └── example
│   │   │           └── demo
│   │   │               └── SpringBootDemoApplication.java
│   │   └── resources
│   │       ├── application.properties
│   └── test
│       └── java
│           └── com
│               └── example
│                   └── demo
│                       └── SpringBootDemoApplicationTests.java
└── pom.xml

Key Files and Directories

  • src/main/java: Contains your main application code.
  • src/main/resources: Contains configuration files (e.g., application.properties).
  • src/test/java: Contains your test code.
  • pom.xml: Maven configuration file.

Step 4: Writing a Simple REST Controller

  1. Create a Controller:
    • In the src/main/java/com/example/demo directory, create a new package named controller.
    • In the controller package, create a new Java class named HelloController.

HelloController.java

package com.example.demo.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api")
public class HelloController {

    @GetMapping("/hello")
    public String hello() {
        return "Hello, World!";
    }
}

Step 5: Running the Application

  1. Run as Spring Boot App:
    • Open the SpringBootDemoApplication class in the src/main/java/com/example/demo directory.
    • Click the green Run button in the top right corner of the editor or right-click the class and select Run 'SpringBootDemoApplication'.

IntelliJ IDEA will start the Spring Boot application, and you should see output in the Run tab indicating that the application has started successfully.

Step 6: Testing the Application

  1. Open a Web Browser:
    • Open your web browser and navigate to http://localhost:8080/api/hello.

You should see the response Hello, World! from your Spring Boot application.

Conclusion

In this tutorial, we have walked through the process of creating a Spring Boot project in IntelliJ IDEA. We created a new Spring Boot project using Spring Initializr, added a simple REST controller, and ran the application. By following these steps, you should now have a basic Spring Boot REST API application running in IntelliJ IDEA. This setup provides a solid foundation for developing more complex Spring Boot applications.


Comments