Spring Boot Tutorial

Spring Boot - Software Setup and Configuration (STS/Eclipse/IntelliJ)

Prerequisite (Spring Core Concepts)

Spring Boot Core

Spring Boot with REST API

Spring Boot with Database and Data JPA

Spring Boot with Kafka

Spring Boot with AOP

Spring Boot - Hello World Example

Creating a "Hello World" example using Spring Boot is straightforward thanks to Spring Initializr and the Spring Boot auto-configuration features. Here's a step-by-step guide:

1. Project Initialization:

You can initialize a new Spring Boot project using:

  • Spring Initializr: Visit https://start.spring.io/ and choose your project options. At a minimum, select "Web" under the dependencies section to include Spring Web support. Download the generated project and unzip it.

  • Using Spring Boot CLI: If you've installed the Spring Boot CLI, you can run the following command:

    spring init --dependencies=web my-hello-world
    

2. Project Structure:

Once you've initialized the project, you'll find a predefined structure. The main class will be in src/main/java within the package you defined in Initializr. This main class is the entry point of the Spring Boot application.

3. Code Your Hello World:

In the src/main/java directory, modify or create a new controller class:

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!";
    }

}

The above code creates a REST endpoint that responds to GET requests on "/hello" and returns the string "Hello, World!".

4. Running Your Application:

Navigate to the root directory of your project in your terminal and run:

./mvnw spring-boot:run

This command uses Maven (packaged with the project) to start your Spring Boot application.

5. Access Your Hello World:

Once the application is running, open a web browser or use a tool like curl to access your "Hello World" endpoint:

curl http://localhost:8080/hello

You should see the output:

Hello, World!

6. Stopping Your Application:

To stop the application, you can simply press CTRL + C in the terminal where the application is running.

Wrap-up:

And that's it! You've just created a simple "Hello World" application using Spring Boot. The advantage of using Spring Boot lies in its simplicity, convention-over-configuration approach, and its extensive set of features and integrations.