How to Create a Spring Boot Project in Eclipse IDE | Spring Initializr | Build Spring Boot REST API

Spring Boot is a Java framework for building REST APIs, Web applications, and microservices in Java. Eclipse IDE is one of the most popular tools for Java development, and combined with Spring Boot, it becomes a powerful environment for creating, running, and managing projects. This guide will show you how to create a Spring Boot project using Spring Initializr, import it into Eclipse IDE, and build a simple REST API.

YouTube Video

We recommend watching the YouTube video below to learn how to create a Spring Boot project in Eclipse IDE using Spring Initializr and build Spring Boot REST API:

What You Need Before Starting

  1. Java Development Kit (JDK 17 or higher):
    Spring Boot 3 requires Java 17 or above.

    • Verify your Java version:
      java -version
      
    • Download the JDK from Oracle or OpenJDK.
  2. Eclipse IDE:
    Download and install the latest version of Eclipse IDE for Enterprise Java Developers from https://www.eclipse.org/downloads/.

Step 1: Create a Spring Boot Project Using Spring Initializr

Spring Initializr is a web-based tool that helps generate Spring Boot projects with the necessary configurations and dependencies.

  1. Open your browser and go to Spring Initializr.
  2. Fill in the project details:
    • Group: Enter your organization or package name, e.g., com.example.
    • Artifact: Enter your project name, e.g., spring-boot-rest-api.
    • Language: Select Java.
    • Packaging: Select Jar (default).
    • Java Version: Choose 17 or later.
  3. Add dependencies:
    • Click Add Dependencies and select:
      • Spring Web: To build REST APIs.
      • Spring Boot DevTools: For hot-reloading during development.
      • Spring Data JPA: To interact with databases.
      • H2 Database: For an in-memory database (optional).
  4. Click Generate to download the project as a ZIP file.

Step 2: Import the Spring Boot Project into Eclipse

  1. Open Eclipse IDE:

    • Launch Eclipse and choose a workspace where your projects will be stored.
  2. Import the Project:

    • Go to File > Import.
    • Select Existing Maven Projects (since the Spring Boot project uses Maven).
    • Click Next.
  3. Locate the Project Folder:

    • Browse to the folder where you extracted the ZIP file from Spring Initializr.
    • Click Finish to import the project.
  4. Wait for Eclipse to Download Dependencies:

    • Eclipse will automatically download the necessary dependencies mentioned in the pom.xml file.

Step 3: Explore the Project Structure

Once the project is imported, you’ll see a structure similar to this:

spring-boot-rest-api/
├── src/
│   ├── main/
│   │   ├── java/com/example/springbootrestapi/
│   │   │   └── SpringBootRestApiApplication.java
│   │   └── resources/
│   │       ├── application.properties
│   ├── test/
│       └── java/
├── pom.xml
  • SpringBootRestApiApplication.java: The main entry point of your Spring Boot application.
  • application.properties: Configuration file for your application.
  • pom.xml: Maven file for managing dependencies.

Step 4: Build a Simple REST API

Now, let’s add a REST controller to handle HTTP requests.

  1. Create a New Java Class:

    • Right-click on the package com.example.springbootrestapi in the src/main/java folder.
    • Select New > Class.
    • Name it HelloController and click Finish.
  2. Add REST API Code:

    • Open the HelloController.java file and add the following code:
      package com.example.springbootrestapi;
      
      import org.springframework.web.bind.annotation.GetMapping;
      import org.springframework.web.bind.annotation.RestController;
      
      @RestController
      public class HelloController {
      
          @GetMapping("/")
          public String sayHello() {
              return "Welcome to Spring Boot!";
          }
      }

Step 5: Run the Spring Boot Application

  1. Run the Application:

    • Open the SpringBootRestApiApplication.java file in the src/main/java/com/example/springbootrestapi folder.
    • Right-click on the file and select Run As > Java Application.
  2. Verify the Application:

    • Once the application starts, the console will display a message like:
      Tomcat started on port(s): 8080 (http)
      
    • Open your browser and go to http://localhost:8080/.
    • You should see the response:
      Welcome to Spring Boot!

Step 6: Customize Application Properties (Optional)

You can modify the application.properties file in the src/main/resources folder to customize your application settings. For example:

  • Change the server port:
    server.port=8081

Step 7: Test Your REST API

For more advanced testing:

  1. Postman: Use Postman to send HTTP GET, POST, PUT, or DELETE requests to your API.
  2. Browser: You can use your browser to make simple GET requests.

Conclusion

You’ve successfully created a Spring Boot project using Spring Initializr, imported it into Eclipse IDE, and built a simple REST API. With these tools, you can start developing powerful Java applications. Keep experimenting with more features of Spring Boot, like connecting to a database or creating complex endpoints. Happy coding!

Comments