How to Create a Spring Boot 3 Project in Spring Tool Suite (STS) | Build Spring Boot 3 REST API in STS

Spring Tool Suite (STS) is a powerful IDE designed specifically for Spring developers. It simplifies creating, running, and managing Spring Boot projects, including the latest Spring Boot 3. In this guide, you will learn how to create a Spring Boot 3 project in STS and build a simple REST API step by step.

YouTube Video

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

Why Use Spring Tool Suite (STS)?

  • Built for Spring: It’s tailored for Spring development with pre-installed plugins for Spring Boot.
  • Simplifies Development: Comes with tools like Spring Initializr integration, autocompletion, and debugging support.
  • Lightweight and Free: A focused IDE for Spring applications that is completely free.

Prerequisites

Before you begin, ensure the following are installed:

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

    • Verify your Java installation:
      java -version
      
    • Download the JDK from Oracle or OpenJDK.
  2. Spring Tool Suite (STS):
    Download the latest version of STS from https://spring.io/tools. Install it like any other application and ensure it’s working correctly.

Step 1: Create a Spring Boot 3 Project in STS

  1. Open STS: Launch Spring Tool Suite and select a workspace where your projects will be stored.

  2. Create a New Project:

    • Go to File > New > Spring Starter Project.
    • Alternatively, click the Spring Starter Project button on the toolbar.
  3. Configure the Project:

    • Project Name: Enter a name (e.g., springboot3-rest-api).
    • Type: Select Maven (recommended).
    • Group ID: Enter your package group (e.g., com.example).
    • Artifact ID: Enter your project artifact name (e.g., springboot3-rest-api).
    • Java Version: Select 17 or later.
    • Click Next.
  4. Add Dependencies:

    • Select the following dependencies:
      • Spring Web: To build REST APIs.
    • Click Finish.

STS will automatically generate the project structure and download the required dependencies.

Step 2: Explore the Project Structure

Once the project is created, you’ll see the following structure:

springboot3-rest-api/
├── src/
│   ├── main/
│   │   ├── java/com/example/springboot3restapi/
│   │   │   └── Springboot3RestApiApplication.java
│   │   └── resources/
│   │       ├── application.properties
│   ├── test/
│       └── java/
├── pom.xml
  • Springboot3RestApiApplication.java: The main class containing the entry point for your application.
  • application.properties: Configuration file for application settings.
  • pom.xml: The Maven file that manages your project’s dependencies.

Step 3: Build a Simple REST API

  1. Create a REST Controller:

    • Right-click on the src/main/java/com/example/springboot3restapi/ folder.
    • Select New > Class and name it HelloController.
  2. Add Code to the Controller:

    • Open the HelloController.java file and add the following code:
      package com.example.springboot3restapi;
      
      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 3!";
          }
      }

Step 4: Run the Spring Boot Application

  1. Run the Application:

    • Open the Springboot3RestApiApplication.java file.
    • Right-click on the file and select Run As > Spring Boot App.
  2. Verify the Application:

    • After the application starts, the console will display logs indicating that Tomcat has started on port 8080.
    • Open your browser and navigate to:
      http://localhost:8080/
      
    • You should see the response:
      Welcome to Spring Boot 3!

Step 5: 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 6: Test the REST API

You can test the REST API using the following methods:

  1. Browser: Open your browser and go to http://localhost:8080/.
  2. Postman: Use Postman to send a GET request to http://localhost:8080/.
  3. cURL: Run this command in the terminal:
    curl http://localhost:8080/

Conclusion

Congratulations! You’ve successfully created a Spring Boot 3 project in Spring Tool Suite (STS) and built a simple REST API. This setup provides a solid foundation for creating robust Java applications. As you gain confidence, you can expand your application by adding more endpoints, services, and advanced features. Happy coding!

Comments