How to Create a Spring Boot 3 Project in Spring Tool Suite (STS) | Build Spring Boot 3 REST API in STS
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:
Java Development Kit (JDK 17 or higher):
Spring Boot 3 requires Java 17 or later.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
Open STS: Launch Spring Tool Suite and select a workspace where your projects will be stored.
Create a New Project:
- Go to File > New > Spring Starter Project.
- Alternatively, click the Spring Starter Project button on the toolbar.
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.
- Project Name: Enter a name (e.g.,
Add Dependencies:
- Select the following dependencies:
- Spring Web: To build REST APIs.
- Click Finish.
- Select the following dependencies:
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
Create a REST Controller:
- Right-click on the
src/main/java/com/example/springboot3restapi/
folder. - Select New > Class and name it
HelloController
.
- Right-click on the
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!"; } }
- Open the
Step 4: Run the Spring Boot Application
Run the Application:
- Open the
Springboot3RestApiApplication.java
file. - Right-click on the file and select Run As > Spring Boot App.
- Open the
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!
- After the application starts, the console will display logs indicating that Tomcat has started on port
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:
- Browser: Open your browser and go to
http://localhost:8080/
. - Postman: Use Postman to send a GET request to
http://localhost:8080/
. - 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
Post a Comment
Leave Comment