How to make first Spring boot application with example

Set up the development environment:

a. Install the latest version of Java Development Kit (JDK)

b. Download and install an Integrated Development Environment (IDE) like Eclipse or IntelliJ IDEA

c. Install the Spring CLI tool by following the instructions from the official Spring website.


Create a new Spring Boot project using Spring CLI or using the Spring Initializer website:

a. Using Spring CLI:

i. Open the command line or terminal window

ii. Type “spring init –dependencies=web my-first-spring-boot-app” to create a new project with web dependency

b. Using Spring Initializer:

i. Open the Spring Initializer website

ii. Choose the required dependencies and click on Generate Project.


Import the project into your IDE:

a. If you used Spring CLI, import the project as a Maven project

b. If you used Spring Initializer, extract the ZIP file and import it into your IDE as a Maven project.


Write the code:

a. Go to the main class file and add the following code:


typescript

Copy code

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;


@SpringBootApplication

public class MyFirstSpringBootApp {

    public static void main(String[] args) {

        SpringApplication.run(MyFirstSpringBootApp.class, args);

    }

}

Add a REST endpoint:

a. Create a new class and add the following code:

kotlin

Copy code

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

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


@RestController

public class HelloWorldController {

    @GetMapping("/hello")

    public String helloWorld() {

        return "Hello World!";

    }

}

Run the application:

a. Right-click on the main class file and select “Run as Java Application”

b. Open a web browser and navigate to “http://localhost:8080/hello” to see the response from the REST endpoint.

This is how you can create your first Spring Boot application. You can now start building more complex applications using Spring Boot.



Comments