Build Your First Spring Boot REST API

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

  1. Open Spring Initializr:
  2. 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)
  3. Select Dependencies:
    • Add the following dependencies:
      • Spring Web
  4. Generate and Download:
    • Click the “Generate” button to download the project as a ZIP file.

Importing the Project into IntelliJ IDEA

  1. Open IntelliJ IDEA:
    • Launch IntelliJ IDEA from your installed applications.
  2. 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.
  3. 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

  1. Create a New Package:
    • In the src/main/java/com/company/api directory, create a new package named controller.
  2. 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

HelloWorldController Class

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

  1. 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 @Controller and @ResponseBody.
  2. Method:
    • @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

  1. Open Application Class:
    • Navigate to the main application class (annotated with @SpringBootApplication).
  2. 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.
  3. Verify the Application:
    • Open a web browser and go to http://localhost:8080/hello.
    • You should see the message “Hello, World!”.
    • Build Your First Spring Boot REST API

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.

Leave a Comment

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

Scroll to Top