Topic 1:- 5 steps of first ‘Hello world’ application in Spring Boot

image 2

Play Store Application link – Spring Boot in 13 steps – App on Google Play

Spring Boot Introduction:

In this guide, we will walk through the basics of Spring Boot, including setting up a project and creating a simple REST API. Let’s dive straight into the code.

Github Project link – https://github.com/kuldeep101990/SpringBoot


Step 1: Setting Up a Spring Boot Project

Using Spring Initializr

  1. Go to https://start.spring.io/.
  2. Fill in the details:
    • Project: Maven
    • Language: Java
    • Spring Boot: 3.x.x
    • Dependencies: Spring Web
  3. Click Generate, and extract the downloaded ZIP file.

Import into IDE

  • Open your IDE (e.g., IntelliJ or Eclipse).
  • Import the extracted project as a Maven project.


Step 2: Writing the “Hello World” REST API

1. Main Application Class & Controller class

The main class is automatically created. Add the @RestController annotation to make it handle HTTP requests.

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

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

@RestController
public class Controller {

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

Step 4: Running the Application

  1. Open the terminal in your IDE.
  2. Run the following command: mvn spring-boot:run
  3. Alternatively, run the DemoApplication class directly from your IDE.

Step 5: Testing the API

  1. Open a browser or Postman.
  2. Visit the URL: http://localhost:8080/
  3. You should see the response: Hello, World!


Conclusion

Congratulations! You’ve successfully:

  1. Set up a Spring Boot project.
  2. Created a REST API.

Leave a Reply

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