Spring Boot REST API Returns List of Java Beans (JSON)

Introduction

Continuing from the previous chapter, where we created a Spring Boot REST API that returns a single Java Bean. In this chapter, we will build a Spring Boot REST API that returns a List of Java Beans (JSON). This is useful for scenarios where you need to return a collection of data objects, such as a list of students, products, or orders.

Creating the REST Controller to Return a List

Step 1: Modify the Controller Class

  1. Update the Controller Class:
    • In the src/main/java/com/company/restapi/controller directory, open the StudentController class.
    • Modify the class to include a new endpoint that returns a list of Student objects:
package com.company.restapi.controller;

import com.company.restapi.model.Student;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;

@RestController
public class StudentController {

    @GetMapping("/student")
    public Student getStudent() {
        return new Student(1L, "Ramesh", "Fadatare", "ramesh.fadatare@example.com");
    }

    @GetMapping("/students")
    public List<Student> getAllStudents() {
        return Arrays.asList(
                new Student(1L, "Ramesh", "Fadatare", "ramesh.fadatare@example.com"),
                new Student(2L, "Suresh", "Kumar", "suresh.kumar@example.com"),
                new Student(3L, "Mahesh", "Yadav", "mahesh.yadav@example.com")
        );
    }
}

Explaining the Updated Controller Class

  • @GetMapping("/students") Annotation: Maps HTTP GET requests to the /students URL to the getAllStudents method.
  • getAllStudents Method: Returns a list of Student objects. This example uses Arrays.asList to create a list of sample Student objects.

Running the Application

Step 2: Run Your Spring Boot Application

  1. Open Application Class:
    • Ensure that the main application class (annotated with @SpringBootApplication) is open.
  2. Run the Application:
    • In IntelliJ IDEA, right-click the main application class and select “Run ‘SpringbootRestApiApplication'”.
    • 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/students.
    • You should see a JSON representation of the list of Student objects:
[
    {
        "id": 1,
        "firstName": "Ramesh",
        "lastName": "Fadatare",
        "email": "ramesh.fadatare@example.com"
    },
    {
        "id": 2,
        "firstName": "Suresh",
        "lastName": "Kumar",
        "email": "suresh.kumar@example.com"
    },
    {
        "id": 3,
        "firstName": "Mahesh",
        "lastName": "Yadav",
        "email": "mahesh.yadav@example.com"
    }
]

Output

Spring Boot REST API Returns List of Java Beans (JSON)

Conclusion

In this chapter, we have extended the Spring Boot REST API to return a list of Java Beans as JSON. We updated the controller to include a new endpoint that returns a list of Student objects and verified the application by running it and checking the JSON output. This example demonstrates how to handle collections of data objects in a Spring Boot REST API.

Leave a Comment

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

Scroll to Top