How to Create a Spring Boot Project in Eclipse IDE

In this tutorial, we will walk through the process of creating a Spring Boot project using Eclipse IDE. We will cover setting up Eclipse, 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
  • Eclipse IDE installed
  • Maven installed (optional, but recommended)

Step 1: Installing the Spring Tools Suite (STS) Plugin

To get started with Spring Boot in Eclipse, it's helpful to install the Spring Tools Suite (STS) plugin, which provides comprehensive support for developing Spring applications.

  1. Open Eclipse: Launch your Eclipse IDE.

  2. Install STS Plugin:

    • Go to Help -> Eclipse Marketplace.
    • In the Find box, type Spring Tools 4 and press Enter.
    • Click the Go button, and in the search results, find Spring Tools 4 (aka Spring Tool Suite 4).
    • Click Install and follow the prompts to complete the installation.
  3. Restart Eclipse: After the installation is complete, restart Eclipse for the changes to take effect.

Step 2: Creating a New Spring Boot Project

  1. Open Spring Starter Project Wizard:

    • Go to File -> New -> Other.
    • In the Select a wizard dialog, type Spring Starter Project and select it, then click Next.
  2. Configure Project Metadata:

    • Name: Enter the name of your project (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.
  3. 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.
  4. Review and Finish:

    • Review your project details and click Finish.

Eclipse 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 Eclipse 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:
    • Right-click on the main class SpringBootDemoApplication in the src/main/java/com/example/demo directory.
    • Select Run As -> Spring Boot App.

Eclipse will start the Spring Boot application, and you should see output in the Console 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 Eclipse IDE. We installed the STS plugin, created a new Spring Boot project, added a simple REST controller, and ran the application. By following these steps, you should now have a basic Spring Boot application running in Eclipse. This setup provides a solid foundation for developing more complex Spring Boot applications.


Comments