Cover Image for Spring Boot Example-STS
99 views

Spring Boot Example-STS

Creating a Spring Boot project using Spring Tool Suite (STS) is straightforward. Here’s a step-by-step guide to creating a simple Spring Boot application in STS:

Step 1: Install Spring Tool Suite (STS)

If you haven’t already installed STS, you can download it from the official website: https://spring.io/tools. Follow the installation instructions for your operating system.

Step 2: Create a New Spring Boot Project

  1. Open Spring Tool Suite.
  2. Go to the “File” menu and select “New” -> “Spring Starter Project.”

Step 3: Configure the Project

In the “New Spring Starter Project” wizard, you’ll need to configure your project settings:

  • Project: Choose “Maven” or “Gradle” for your build system.
  • Language: Select “Java” or “Kotlin” as the programming language.
  • Packaging: Choose “Jar” or “War,” depending on your deployment needs.
  • Java Version: Select your desired Java version.
  • Group: Specify the package name for your project, typically in reverse domain format (e.g., com.example).
  • Artifact: Enter the project’s name (e.g., my-spring-boot-app).
  • Version: Leave it as the default.

Step 4: Add Dependencies

In the “Dependencies” section of the wizard, you can search for and select dependencies that you want to include in your Spring Boot project. For a basic “Hello World” example, you can add the “Spring Web” dependency. To do this:

  1. Type “web” in the search box.
  2. Select the “Spring Web” checkbox.

Step 5: Finish Project Configuration

Click the “Next” button to review the project’s configuration. Ensure that everything looks correct.

Step 6: Finish Project Creation

Click the “Finish” button to create your Spring Boot project. STS will generate the project structure and download the necessary dependencies.

Step 7: Write the Spring Boot Code

STS will automatically open the project in the IDE. You can now start writing your Spring Boot application code.

  1. In the src/main/java directory, you’ll find the main application class. It should be named something like MySpringBootApplication.java. This class contains the main() method and is the entry point of your Spring Boot application.
  2. Create a new Java class for your controller (e.g., HelloController.java) with the following code to create a simple RESTful endpoint that returns “Hello, World!”:
   package com.example.my_spring_boot_app;

   import org.springframework.web.bind.annotation.GetMapping;
   import org.springframework.web.bind.annotation.RestController;

   @RestController
   public class HelloController {

       @GetMapping("/hello")
       public String sayHello() {
           return "Hello, World!";
       }
   }

Step 8: Run the Application

Right-click on the main application class (e.g., MySpringBootApplication.java) and select “Run As” -> “Spring Boot App” from the context menu.

Step 9: Access the “Hello World” Endpoint

Open a web browser or use a tool like curl to access the “Hello World” endpoint:

  • Open a web browser and go to http://localhost:8080/hello (assuming your application is running on the default port).
  • You should see the “Hello, World!” message displayed in your browser.

That’s it! You’ve created a simple Spring Boot application using Spring Tool Suite. You can build upon this foundation to develop more complex Spring Boot applications.

YOU MAY ALSO LIKE...

The Tech Thunder

The Tech Thunder

The Tech Thunder


COMMENTS