Build Your First Spring Boot REST API

Ramesh Fadatare
3 min readJan 11, 2025

--

Introduction

In this quick tutorial, we are going to build our first simple “Hello World” REST API using Spring Boot.

We are going to use:

  • Java 23
  • Spring Boot 3.4.x
  • Maven
  • IntelliJ IDEA
  • Browser

Check out my free 15-hour Spring Boot course on my YouTube channel: Spring Boot 3 Full Free Course in 15 Hours.

https://www.youtube.com/c/javaguides

Creating a New Spring Boot Project

Open Spring Initializr:

Configure Project Metadata:

  • Group: com.company
  • Artifact: first-springboot-api
  • Name: first-springboot-api
  • Description: First Spring Boot REST API
  • Package name: com.company.api
  • Packaging: Jar
  • Java Version: 21 (or the latest version available)

Select Dependencies:

  • Add the following dependencies:
  • Spring Web

Generate and Download:

  • Click the “Generate” button to download the project as a ZIP file.

Importing the Project into IntelliJ IDEA

Open IntelliJ IDEA:

  • Launch IntelliJ IDEA from your installed applications.

Import the Project:

  • On the welcome screen, click “Open or Import”.
  • Navigate to the directory where you downloaded the Spring Initializr ZIP file.
  • Select the ZIP file and click “Open”.
  • IntelliJ IDEA will unzip the file and import the project.

Configure Maven (if needed):

  • IntelliJ IDEA will automatically detect the Maven build file (pom.xml) and import the project.
  • If prompted, confirm any necessary Maven configurations.

Implementing the “Hello World” REST API

Creating a Controller Class

Create a New Package:

  • In the src/main/java/com/company/api directory, create a new package named controller.

Create the Controller Class:

  • Inside the controller package, create a new class named HelloWorldController.
  • Add the following code to the HelloWorldController class:
package com.company.api.controller;

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!";
}
}

Explaining the REST API Source Code

The HelloWorldController class is a simple Spring Boot controller that defines a REST API endpoint.

@RestController: This annotation is used to define a controller and indicates that the return value of the methods should be written directly to the HTTP response body. It's a combination of @Controller and @ResponseBody.

@GetMapping("/hello"): This annotation maps HTTP GET requests to the /hello endpoint. When a GET request is made to this URL, the helloWorld method is invoked.

public String helloWorld(): This method returns the string "Hello, World!" which is sent as the HTTP response body.

Running the Application

Open Application Class:

  • Navigate to the main application class (annotated with @SpringBootApplication).

Run the Application:

  • In IntelliJ IDEA, right-click the main application class and select “Run ‘DemoAPIApplication’”.
  • Alternatively, open a terminal, navigate to the project directory, and run mvn spring-boot:run.

Verify the Application:

Conclusion

In this tutorial, you have learned how to create a simple Spring Boot project and implement a “Hello World” REST API. This basic example provides a foundation for building more complex REST APIs using Spring Boot.

--

--

No responses yet