Springboot hello app - demo project

Here’s a complete beginner-friendly guide to create a simple Spring Boot application step-by-step — using only annotations, no XML, and designed for learning key Spring Boot concepts like:

  • @SpringBootApplication

  • @RestController

  • @GetMapping

  • @Autowired with @Service


✅ Project: springboot-helloapp

This app exposes an API:

GET http://localhost:8080/hello


Response:

{ "message": "Hello, Spring Boot!" }



🔧 Step-by-Step Setup


🔹 Step 1: Go to Spring Initializr

🌐 Visit: https://start.spring.io

Fill these:

Field

Value

Project

Maven

Language

Java

Spring Boot

3.2.x (latest stable)

Group

com.example

Artifact

springboot-helloapp

Name

springboot-helloapp

Packaging

Jar

Java

17 or 21

Add Dependencies:

  • ✅ Spring Web

Then click "Generate"
Unzip the project and open it in VSCode or IntelliJ


🔹 Step 2: Project Structure

src/

 └── main/

     ├── java/

     │   └── com.example.springboothelloapp/

     │       ├── SpringbootHelloappApplication.java

     │       ├── controller/HelloController.java

     │       └── service/HelloService.java

     └── resources/

         └── application.properties



🔹 Step 3: Main Application Class

📄 SpringbootHelloappApplication.java

package com.example.springboothelloapp;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringbootHelloappApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootHelloappApplication.class, args);
    }
}



🔹 Step 4: Create HelloService

📄 service/HelloService.java

package com.example.springboothelloapp.service;

import org.springframework.stereotype.Service;

@Service
public class HelloService {
    public String getMessage() {
        return "Hello, Spring Boot!";
    }
}



🔹 Step 5: Create HelloController

📄 controller/HelloController.java

package com.example.springboothelloapp.controller;

import com.example.springboothelloapp.service.HelloService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collections;
import java.util.Map;

@RestController
public class HelloController {

    private final HelloService helloService;

    // Constructor injection (recommended)
    public HelloController(HelloService helloService) {
        this.helloService = helloService;
    }

    @GetMapping("/hello")
    public Map<String, String> sayHello() {
        return Collections.singletonMap("message", helloService.getMessage());
    }
}



🔹 Step 6: Run the App

In terminal:

./mvnw spring-boot:run


Or in VSCode/IntelliJ:
Right-click → Run SpringbootHelloappApplication.main()


🔹 Step 7: Test the Endpoint

Go to:
http://localhost:8080/hello

Response:

{ "message": "Hello, Spring Boot!" }



✅ Summary of Concepts Used

Concept

Usage

@SpringBootApplication

Main application class

@RestController

Exposes HTTP endpoints

@Service

Business logic class

@GetMapping

Maps HTTP GET request

@Autowired (implicit via constructor)

Injects service




Distributed by Gooyaabi Templates | Designed by OddThemes