Create Spring Boot REST API returns List

In this post, we will build a simple Spring boot REST API which returns List object as JSON to the client.

Spring Boot is an opinionated framework that helps developers build Spring-based applications quickly and easily. The main goal of Spring Boot is to quickly create Spring-based applications without requiring developers to write the same boilerplate configuration again and again.

Let's create a Spring boot application step by step.

1. Create Spring Boot Project

Spring Boot provides a web tool called to bootstrap an application quickly. Just go to and generate a new spring boot project.

Use the below details in the Spring boot creation:

Project Name: springboot-first-app

Project Type: Maven

Choose dependencies: Spring Web

Package name:

2. Maven Dependencies

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="" xmlns:xsi=""
		<relativePath/> <!-- lookup parent from repository -->
	<description>Spring Boot First Application</description>




3. Create Spring Boot REST API returns List

Let's first create a Student java bean class that the REST API want to return List of student objects to the client:

public class Student {
	private String firstName;
	private String lastName;
	public Student(String firstName, String lastName) {
		this.firstName = firstName;
		this.lastName = lastName;
	public String getFirstName() {
		return firstName;
	public void setFirstName(String firstName) {
		this.firstName = firstName;
	public String getLastName() {
		return lastName;
	public void setLastName(String lastName) {
		this.lastName = lastName;
Let's create the StudentController class and the below code to it:
import java.util.ArrayList;
import java.util.List;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

public class StudentController {

	public List<Student> getStudents(){		
		List<Student> students = new ArrayList<>();
		students.add(new Student("Ramesh", "Fadatare"));
		students.add(new Student("Tony", "Cena"));
		students.add(new Student("Sanjay", "Jadhav"));
		students.add(new Student("Ram", "Jadhav"));
		students.add(new Student("Umesh", "Fadatare"));
		return students;
  • The above code uses Spring 4’s new @RestController annotation, which marks the class as a controller where every method returns a domain object instead of a view. It’s shorthand for @Controller and @ResponseBody rolled together.
  • @GetMapping annotation for mapping HTTP GET requests onto specific handler methods. Specifically, @GetMapping is a composed annotation that acts as a shortcut for @RequestMapping(method = RequestMethod.GET).
  • The List of student objects must be converted to JSON. Thanks to Spring’s HTTP message converter support, you don’t need to do this conversion manually. Because Jackson 2 is on the classpath, Spring’s MappingJackson2HttpMessageConverter is automatically chosen to convert the List of student objects to JSON.

4. Run Spring Boot Application

The below class SpringbootFirstAppApplication is the entry point that sets up the Spring Boot application. The @SpringBootApplication annotation enables auto-configuration and component scanning.


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

public class SpringbootFirstAppApplication {
	public static void main(String[] args) {, args);

Run spring boot application from the IDE:

From your IDE, run the SpringbootFirstAppApplication.main() method as a standalone Java class that will start the embedded Tomcat server on port 8080 and point the browser to http://localhost:8080/.

Run spring boot application using the command line:

Just go to the root directory of the application and type the following command to run it -

$ mvn spring-boot:run

The application will start at Spring Boot’s default tomcat port 8080.

Just hit this link in a browser: http://localhost:8080/students. You will able to see the response of this REST API in the browser.