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 set up a Spring Boot application. However, the easiest way to do it is via Spring Initializer. Open the Spring Initializer page and add the Spring Web as a dependency. While you are at it, Fill out the project metadata as per your need.
After filling the form, Hit Ctrl+Enter or click Generate to download the project template as a ZIP file.
Import Project in IDE
Extract the downloaded project somewhere on 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 the 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 a 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.
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.