Spring ResponseEntity Example




Hey guys in this post, we will discuss Spring ResponseEntity with example.

Overview


ResponseEntity is meant to represent the entire HTTP response. We can control anything that goes into it such as HTTP status code, headers, and body. It gives more flexibility than @RequestBody annotation.

@RestController
public class MyController {

    @GetMapping(path = "/test")
    public ResponseEntity<User> test() {
        User user = new User();
        user.setName("Name 1");

        HttpHeaders responseHeaders = new HttpHeaders();
        // ...
        return new ResponseEntity<>(user, responseHeaders, HttpStatus.OK);
    }
}

We can also use this –

return ResponseEntity.ok().headers(responseHeaders).body(user);

Watch the video


Example on ResponseEntity


Let’s understand ResponseEntity with an example by creating a simple spring boot project

Create spring boot project


There are many different ways to create a spring boot application, you can follow the below articles to create one –

>> Create spring boot application using Spring initializer
>> Create spring boot application in Spring tool suite [STS]
>> Create spring boot application in IntelliJ IDEA

Add the maven dependencies


Open pom.xml and add the following dependencies –

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.4.3</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>in.bushansirgur</groupId>
	<artifactId>responseentity</artifactId>
	<version>v1</version>
	<name>responseentity</name>
	<description>Spring boot response entity annotation</description>
	<properties>
		<java.version>1.8</java.version>
	</properties>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
			<scope>runtime</scope>
			<optional>true</optional>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>

spring-boot-starter-web dependency for building web applications using Spring MVC. It uses the tomcat as the default embedded container.




spring-boot-devtools dependency for automatic reloads or live reload of applications.

Create an entity class


Create Customer.java inside the in.bushansirgur.springboot.entity package and add the following content

package in.bushansirgur.springboot.entity;

public class Customer {
	
	private String name;
	
	private String location;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getLocation() {
		return location;
	}

	public void setLocation(String location) {
		this.location = location;
	}

	public Customer(String name, String location) {
		super();
		this.name = name;
		this.location = location;
	}

	@Override
	public String toString() {
		return "Customer [name=" + name + ", location=" + location + "]";
	}
}

This is just a plain old java class that has private fields, setters, getters, constructors, and tostring methods.

Create a Rest controller


Create CustomerController.java inside the in.bushansirgur.springboot.controller package and add the following content

package in.bushansirgur.springboot.controller;

import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import in.bushansirgur.springboot.entity.Customer;

@RestController
public class CustomerController {
	
	@GetMapping("/customer")
	public ResponseEntity<Customer> getCustomer () {
		Customer c = new Customer("Customer 1", "India");
		HttpHeaders headers = new HttpHeaders();
		headers.add("token", "token value");
		return ResponseEntity.ok().headers(headers).body(c);
	}
}

We will return ResponseEntity<Customer> of the type Customer with status code 200. We are also passing the HttpHeaders to the headers() and we pass the Customer object to the response body() method.

Run the app


Run the application using the below maven command –

mvn spring-boot:run

Open the rest-client and enter the following URL with the following request body

  • http://localhost:8080/customer

Screenshot-2021-03-12-at-3-31-03-PM
Screenshot-2021-03-12-at-3-31-14-PM



Bushan Sirgur

Hey guys, I am Bushan Sirgur from Banglore, India. Currently, I am working as an Associate project in an IT company.

Leave a Reply