RiaChoi Text Logo.
Spring Boot by packt part2.

SpringBoot

Spring Boot by packt part2.

Spring Boot Basics: Making an API request

Ria Choi • September 7th, 2026

Making an API Request

Today, we’re going to make an API request and show the JSON data on the page.

 

This post is based on Packt’s course on Coursera

Spring Boot Foundations - Build a Hello World API with Spring Boot

 

Prerequisite

Before you start building your controller, you need a solid plan for which data you want to get. You’re also going to need to decide the data type beforehand.

This is called an API specification.

Today, we’ll be requesting JSON format data that contains a list of course items.

  • Endpoint: /courses
  • Data: id(long), name(String), author(String)
[
  {
    "id": 1,
    "name": "Learn Spring Boot with Ria Choi",
    "author": "Ria Choi"
  },
  ...
]

 

Controller

We’re going to create a class called “CourseController.java” inside the presentation directory.

The directory has to be located inside the directory where DemoApplication is.

presentation directory
presentation directory
Course Controller
Course Controller

You’ve probably noticed something weird here.
There’s no calling for the public method retrieveAllCourse().

This explains what Spring MVC does. (DispatcherServlet, to be exact)

When Spring Boot is running the application, it scans through classes with the @RestController annotation (with the @ symbol).

And this whole process of Spring Boot calling our code is called IoC(Inversion of Control).

If you want to know more about how Spring Boot works, you can check out this blog post.

CourseController

@RestController
Marks the class as a REST controller. Every method's return value is automatically converted to JSON (or XML) and written directly to the HTTP response body.

@RequestMapping("/courses")
Sets the base URL path for all endpoints in this class. Every method's path is prefixed with /courses.

@GetMapping
Maps HTTP GET requests to this method. Combined with the class-level @RequestMapping, a GET request to /courses will call retrieveAllCourses().

List<Course>
The return type. Spring (via Jackson) automatically serializes this list of Course objects into a JSON array.

Arrays.asList()
A quick way to create a fixed-size List from given elements, used here to return two hardcoded Course objects for testing purposes.

Class

Right after you create your CourseController, you will have some error lines in your code. That is because you haven’t created the “Course.java” class yet.

Now we’ll add another directory, “domain” which contains the Course.java class.

domain folder
Domain Folder

Let’s take a look at the file.

Course.java
Course.java

Course

Getters (getId(), getName(), getAuthor())
Required for JSON serialization. Jackson uses these getter methods to read each field's value and build the JSON output — without them, the fields would be missing from the response.

Constructor (Course(long id, String name, String author))
Lets you create a Course object by passing values directly, e.g. new Course(1, "Learn Spring Boot with Ria Choi", "Ria Choi"), instead of creating an empty object and setting fields one by one.

 

Now let's open it up in your browser.

Put http://localhost:8080/courses in the search bar. - 8080 is the default port.

/courses
/courses

You’ll end up seeing something like this: The JSON response sent by the server (Spring Boot) to the browser when you made a GET request to /courses.

 

The Lombok way

package com.example.demo.domain;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.ToString;

@Getter
@AllArgsConstructor
@ToString
public class Course {
    private long id;
    private String name;
    private String author;
}

You see how clean the Course.java class got.

Each annotation is replaced by @Getter, @AllArgsConstructor, and @ToString.

This code does the same job, but it’s way simpler.

  • Lombok: A library that automatically generates repetitive boilerplate code at compile time using annotations.
  • @Getter: Automatically generates a getter method for every field (e.g. getId(), getName()).
  • @AllArgsConstructor: Automatically generates a constructor that takes all fields as parameters.
  • @ToString: Automatically generates a toString() method that returns a readable string of the object's field values.

Share

Related contents

Spring Boot by packtSpring Boot by packt

August 30th, 2026