How to create a Spring Boot application

siddharth1006

According to the official Spring Boot website,

Spring Boot is a software that makes it easy to create stand-alone, production-grade Spring based Applications that you can “just run”.

We take an opinionated view of the Spring platform and third-party libraries so you can get started with minimum fuss.

Creating Your First Spring Boot Application

In this tutorial, we’ll guide you through the process of creating your first Spring Boot application.

Prerequisites

Before we begin, make sure you have the following:

Step 1: Set Up a New Spring Boot Project

  1. Open your preferred Integrated Development Environment (IDE).
  2. Create a new Spring Boot project.
    • You can use Spring Initializer (https://start.spring.io/) or your IDE’s built-in Spring Boot project creation tool as follows:
    • Select the dependencies you need (e.g., Spring Web, Spring Boot DevTools, etc.).

Step 2: Create a Spring Boot Application Class

  1. Create a new Java class (e.g., MyApplication) in the src/main/java/com/example/demo directory.
  2. Add the @SpringBootApplication annotation at the top of your class.
    @SpringBootApplication
    public class MyApplication {
        public static void main(String[] args) {
            SpringApplication.run(MyApplication.class, args);
        }
    }

Step 3: Create a Simple Controller

  1. Create a new Java class (e.g., HelloController) in the src/main/java/com/example/demo directory.
  2. Add the @RestController and @RequestMapping annotations.
    @RestController
    public class HelloController {
        @RequestMapping("/")
        public String hello() {
            return "Hello, World!";
        }
    }

Step 4: Run Your Spring Boot Application

  1. Right-click on the MyApplication class and select “Run MyApplication”.
  2. Alternatively, you can run the application from the command line using Maven: mvn spring-boot:run

Step 5: Access Your Application

Open a web browser and go to http://localhost:8080. You should see the message “Hello, World!“.