🏠Spring BootSpring Boot Hello World Tutorial

Spring Boot Hello World Tutorial

Let’s build a Hello World web application using Spring Boot. We will go through step by step to learn how to do this.

Generate A Spring Boot Project

There are multiple ways to setup a Spring Boot application. However, the easiest way to do it via Spring Initializer. Open the Spring Initializer page and add the Spring Web as dependency. While you are at it, Fill out the project metadata as per your need.

Spring Initializer

After filling the form, Hit Ctrl+Enter or click Generate to download the project template as ZIP file.

Import Project in IDE

Extract the downloaded project somewhere in your computer. After that, open Intellij IDEA -> File -> New -> Project from Existing Sources -> Select the directory where you extracted the hello world project.

After selecting, Choose Import Project from External Model -> Maven -> Finish.

You can also use Open-> Select directory -> Ok to directly load any Maven project in Intellij

For other IDEs like eclipse, VSCode etc, You can follow similar steps because a spring boot project is still a maven project.


Hello World Controller

Further, We need to add a controller that will listen and serve Hello World as response. To do that, add the below Controller to your application under the package com.springhow.examples.helloworld.controllers.

@RestController public class HelloWorldController { @RequestMapping("/") String helloWorld() { return "Hello World!"; } }
Code language: JavaScript (javascript)

Spring Boot Hello World Application

To run the Spring boot project, Simply call mvn spring-boot:run. This maven command utilizes Spring Boot Maven plugin to build and run the spring boot project.

You can also start the application from IDE by simply starting the main class HelloWorldSpringBootApplication.

Subsequently, Open the browser and view the URL https://localhost:8080. It returns the “Hello World!” String which was hardcoded in the controller.

Demo for Spring Boot Hello World Web application

Summary

In Short, we learned how to create and run a Spring Boot Hello World web application. The following articles may be the right path to learning Spring Boot.

Similar Posts

Leave a Reply

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