Spring Boot REST API That Returns List of Java Objects in JSON Format
Continuing from the previous guide, where we created a Spring Boot REST API that returns a single Java Bean. In this guide, 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.
Learn everything about Spring Boot: Spring Boot Tutorial.
Prerequisites
This guide is a continuation of Spring Boot REST API That Returns JSON.
Step 1: Modify the Controller Class
Update the Controller Class:
- In the
src/main/java/com/company/restapi/controller
directory, open theStudentController
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 thegetAllStudents
method.getAllStudents
Method: Returns a list ofStudent
objects. This example usesArrays.asList
to create a list of sampleStudent
objects.
Step 2: Run Your Spring Boot Application
Open Application Class:
- Ensure that the main application class (annotated with
@SpringBootApplication
) is open.
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
.
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
Conclusion
In this guide, 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.
Learn everything about Spring Boot: Spring Boot Tutorial.