Spring Boot Hello World Application

Introduction

In this tutorial, we will show you how to create a “hello world” Spring Boot application using Gradle as our build tool. We will also cover how to use the Spring Initializr website to quickly generate a new Spring Boot project and how to import it into both Eclipse IDE and IntelliJ IDEA. By the end of this tutorial, you will have a working Spring Boot application that you can run and modify to fit your needs.

Getting Started

Before we get started, make sure you have the following tools installed on your machine:

  • Java Development Kit (JDK) version 8 or higher
  • Gradle build tool version 4 or higher
  • Eclipse IDE or IntelliJ IDEA

You can download the JDK and Gradle from their respective websites, while Eclipse and IntelliJ IDEA can be downloaded from their official websites as well.

Creating a Spring Boot Hello World Project

To create a new Spring Boot project, we will use the Spring Initializr website. The website provides a user-friendly interface for generating new Spring Boot projects, with options to select the required dependencies, project metadata, and other settings.

  1. Open your web browser and navigate to the Spring Initializr website at start.spring.io.
  2. In the “Project” section, select “Gradle Project” from the dropdown menu.
  3. In the “Language” section, select “Java” from the dropdown menu.
  4. In the “Spring Boot” section, select the latest version of Spring Boot from the dropdown menu. For this tutorial, we will be using Spring Boot 3.0.4.
  5. In the “Project Metadata” section, enter the following information:
  • Group: com.notearena
  • Artifact: demo
  • Name: SpringBootHelloWorld
  • Description: Hello World project for Spring Boot
  • Package Name: com.notearena.demo
  1. In the “Dependencies” section, add the following dependencies:
  • Spring Web
  • Spring Boot DevTools
  1. Click on the “Generate” button to download the generated project as a ZIP file.

Spring boot hello world project

Importing the Project into Eclipse IDE

1. pen Eclipse IDE and select “File” -> “Import” from the menu bar.

2. In the “Import” window, expand the “Gradle” folder and select “Existing Gradle Project”.

3. Click on the “Next” button.

4. In the “Import Gradle Project” window, select the root directory of the project that you downloaded from the Spring Initializr website.

select spring boot project root directory

5. Click on the “Finish” button to import the project.

Finish importing project

6. Once the project is imported, you should see it in the “Package Explorer” view.

Run the application

1. Right-click on the “SpringBootHelloWorldApplication.java” file in the “src/main/java/com/notearena/demo” package and select “Run As” -> “Java Application”.

2. The Spring Boot application should start up and you should see the following output in the console:

Started SpringBootHelloWorldApplication in 2.034 seconds (JVM running for 2.546)
run spring boot app in eclipse

3. Open a web browser and navigate to “http://localhost:8080“. You should see a “Whitelabel Error Page” with an HTTP status code of 404.

whitelable 404 error

Importing the Project into IntelliJ IDEA

  1. Open IntelliJ IDEA and select “File” -> “New” -> “Project from Existing Sources” from the menu bar.
  2. In the “Import Project” window, navigate to the root directory of the project that you downloaded from the Spring Initializr website and select the “build.gradle” file.
  3. Click on the “Open” button to import the project.
  4. Once the project is imported,
  5. you should see it in the “Project” view.
  6. To run the application, right-click on the “Application.java” file in the “src/main/java/com/example/demo” package and select “Run DemoApplication”.
  7. The Spring Boot application should start up and you should see the following output in the console:
  8. Open a web browser and navigate to “http://localhost:8080“. You should see a “Whitelabel Error Page” with an HTTP status code of 404.

Modifying the Application

Now that we have a working Spring Boot application, let’s modify it to output a simple “hello world” message.

  1. Open the “Application.java” file in your IDE.
  2. Replace the contents of the file with the following code:
package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
public class DemoApplication {

	public static void main(String[] args) {
		SpringApplication.run(DemoApplication.class, args);
	}

	@GetMapping("/")
	public String helloWorld() {
		return "Hello, world!";
	}

}
  1. Save the file.
  2. Run the application again.
  3. Open a web browser and navigate to “http://localhost:8080“. You should see the “hello world” message displayed in the browser.

hello world output

Congratulations! You have successfully created a Spring Boot “hello world” application using Gradle as your build tool and imported it into both Eclipse IDE and IntelliJ IDEA. You have also learned how to use the Spring Initializr website to quickly generate a new Spring Boot project with the required dependencies and metadata.

Conclusion

In this tutorial, we have covered the basic steps for creating a Spring Boot “hello world” application using Gradle as our build tool. We have also demonstrated how to use the Spring Initializr website to generate a new Spring Boot project and how to import it into both Eclipse IDE and IntelliJ IDEA. By following these steps, you can quickly create and run a Spring Boot application that can be modified to fit your needs.

Also, see the example code JavaExamples_NoteArena in our GitHub repository. See complete examples in our GitHub repositories.

Follow us on social media
Follow Author