How to Create a Spring Boot Project in Spring Tool Suite (STS) IDE

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

Step 1: Setting Up Spring Tool Suite

Spring Tool Suite (STS) is an IDE specifically designed for developing Spring applications. It includes all the necessary tools to create and manage Spring projects efficiently.

  1. Download and Install STS:

    • Download STS from the Spring Tools website.
    • Follow the installation instructions for your operating system.
  2. Launch STS:

    • Open Spring Tool Suite from your applications menu.

Step 2: Creating a New Spring Boot Project

  1. Open Spring Starter Project Wizard:

    • Go to File -> New -> Spring Starter Project.
  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.

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

STS 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 Spring Tool Suite (STS) IDE. 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 STS. This setup provides a solid foundation for developing more complex Spring Boot applications.


Comments