Introduction
In this chapter, we are going to build our first simple “Hello World” REST API using Spring Boot.
We are going to use:
- Java 21
- Spring Boot 3.2
- Maven
- IntelliJ IDEA
- Browser
Creating a New Spring Boot Project
Using Spring Initializr
- Open Spring Initializr:
- Navigate to Spring Initializr in your web browser.
- 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)
- Group:
- Select Dependencies:
- Add the following dependencies:
- Spring Web
- Add the following dependencies:
- 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.
- IntelliJ IDEA will automatically detect the Maven build file (
Implementing the “Hello World” REST API
Creating a Controller Class
- Create a New Package:
- In the
src/main/java/com/company/apidirectory, create a new package namedcontroller.
- In the
- Create the Controller Class:
- Inside the
controllerpackage, create a new class namedHelloWorldController. - Add the following code to the
HelloWorldControllerclass:
- Inside the
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
HelloWorldController Class
The HelloWorldController class is a simple Spring Boot controller that defines a REST API endpoint.
- Annotations:
@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@Controllerand@ResponseBody.
- Method:
@GetMapping("/hello"): This annotation maps HTTP GET requests to the/helloendpoint. When a GET request is made to this URL, thehelloWorldmethod 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).
- Navigate to the main application class (annotated with
- 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:
- Open a web browser and go to
http://localhost:8080/hello. - You should see the message “Hello, World!”.

- Open a web browser and go to
Conclusion
In this chapter, 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.