Skip to content
Spring Boot

Spring

Spring Boot

Spring

Hello World Program in Spring Boot

admin, January 30, 2025January 31, 2025

1. Setup a Spring Boot Project

You can set up a Spring Boot project using:

  • Spring Initializr (Recommended)
  • Manually (Maven or Gradle)

Using Spring Initializr

  1. Go to Spring Initializr
  2. Select the following:
    • Project: Maven or Gradle
    • Language: Java
    • Spring Boot Version: Latest Stable (e.g., 3.x)
    • Dependencies: Add Spring Web
  3. Click Generate and download the project.
  4. Extract and open it in an IDE (IntelliJ, Eclipse, VS Code).

2. Create the Main Application Class

Spring Boot applications start with a class annotated with @SpringBootApplication.

Main Class (HelloWorldApplication.java)

package com.example.helloworld;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication

public class HelloWorldApplication {

    public static void main(String[] args) {

        SpringApplication.run(HelloWorldApplication.class, args);

    }

}


3. Create a REST Controller

Now, create a REST API to return “Hello, World!”.

Controller (HelloController.java)

package com.example.helloworld;

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

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestControll

@RestController

@RequestMapping(“/api”)

public class HelloController {

    @GetMapping(“/hello”)

    public String sayHello() {

        return “Hello, World!”;

    }

}


4. Run the Application

  • Open a terminal in your project folder and run:

mvn spring-boot:run

OR (if using Gradle)

gradle bootRun

  • You should see logs indicating the server has started on port 8080.

5. Test the API

Open a browser or use Postman and visit:

bash

http://localhost:8080/api/hello

Output:

Hello, World!


Bonus: Running as a JAR

You can package and run your Spring Boot application as a JAR:

mvn clean package

java -jar target/helloworld-0.0.1-SNAPSHOT.jar

Spring Boot

Post navigation

Previous post
Next post

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

©2025 Spring Boot | WordPress Theme by SuperbThemes