Cover Image for Spring Boot Hello World Example
110 views

Spring Boot Hello World Example

Creating a simple “Hello World” example with Spring Boot is a great way to get started with the framework. Here’s a step-by-step guide to creating a basic Spring Boot “Hello World” application:

Step 1: Set Up Your Development Environment

Before you start, make sure you have Java and a build tool (Maven or Gradle) installed on your system. Also, consider using an Integrated Development Environment (IDE) like IntelliJ IDEA or Eclipse for a smoother development experience.

Step 2: Create a New Spring Boot Project

You can create a Spring Boot project manually, or you can use the Spring Initializr web tool to generate a project structure with the required dependencies.

Using Spring Initializr:

  1. Visit the Spring Initializr web page at https://start.spring.io/.
  2. Customize your project by selecting the project type (Maven or Gradle), language (Java or Kotlin), Spring Boot version, and any additional dependencies you may need.
  3. Click the “Generate” button to download a ZIP file containing your project.

Using Your IDE (e.g., IntelliJ IDEA):

  1. Open your IDE and choose to create a new project.
  2. Select “Spring Initializr” as the project type.
  3. Customize the project settings, such as project name, package name, and dependencies. Ensure you select the “Spring Web” dependency for this example.
  4. Click “Next” and complete the project creation process.

Step 3: Write the “Hello World” Code

Now that you have your Spring Boot project set up, you can write the code for your “Hello World” application.

In your Spring Boot project, navigate to the src/main/java directory and create a package for your application. Inside this package, create a Java class, e.g., HelloWorldController.java, with the following code:

package com.example.demo;

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

@RestController
public class HelloWorldController {

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

In this code:

  • @RestController is an annotation that indicates this class is a Spring MVC controller, and its methods return data to the client as JSON or plain text.
  • @GetMapping("/hello") is a mapping annotation that maps the /hello URL path to the sayHello() method.
  • sayHello() is a simple method that returns the “Hello, World!” string.

Step 4: Run Your Spring Boot Application

You can run your Spring Boot application using your IDE or the command line, depending on your preference.

Using Your IDE:

  1. Open the project in your IDE.
  2. Locate the main application class (usually named DemoApplication.java) and run it as a Java application.

Using the Command Line:

  1. Open a terminal.
  2. Navigate to your project’s root directory.
  3. Use the following command to build and run your Spring Boot application: For Maven:
   mvn spring-boot:run

For Gradle:

   ./gradlew bootRun

Step 5: Access the “Hello World” Endpoint

Once your application is running, you can access the “Hello World” endpoint in your web browser or using a tool like curl:

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

Congratulations! You’ve created a simple “Hello World” Spring Boot application. This is just the beginning, and 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