All Ways to HTTP Response using ResponseEntity's Method.

ResponseEntity is a class of Spring Framework that accepts generic types of data. As a result, we can respond with all kinds of data types. ResponseEntity represents the whole HTTP response, including the header, body, and HTTP status. Therefore it is most important to HTTP response. We can use it to fully configure the HTTP response.

Below are the ways to respond to HTTP requests as ResponseEntity.
We have created a simple bean class (Student). Where we defined three variables (id, name, and department).  We will use this class for every endpoint.

Pojo Class
POJO class

1. Simple Way : Here we are responding the full student information using ok() mothod of ResponseEntity class. ResponseEntity.ok() Provides a 200 OK response with the specified body.

  • Method Used : ResponseEntity.ok();

Simple way to response as ResponseEntity

ResponseEntity response in postman
Response in Postman
2. Simple Way 2: This is the same as above way to response to the body with HTTP Status as 200 OK. Just syntax is different. We can use any one of both to send HTTP status as 200 OK.

Simple way to response as ResponseEntity
Return list of student information
ResponseEntity response in postman
Response in Postman


3. Conditionally response with custom HTTP status and response body : It allow to the dynamically generate the HTTP response status and response body based on the application logic.

Conditionally Response using ResponsEntity

ResponseEntity response in postman
Response in Postman

4. Conditionally response using BodyBuilder Interface: The BodyBuiler is an interface that allow to configure of the response body with header and status. It is typically used with the ResponseEntity's method to set up the response body in fluent manner.
Here it is dynamically responding with HTTP status and response body like the above one but we have used here BodyBuilder interface to achieve that. As a result just syntax is different here.

Used Methods :
  • ResponseEntity.badRequest().body()
  • ResponseEntity.status().body()

Conditionally Response in ResponsEntity using BodyBuilder

ResponseEntity response in postman
Response in Postman

5. HTTP response with custom header, HTTP status, and response body using BodyBuilder Interface.
  • Used Method : ResponseEntity.ok().header().body()

Custom Http response header

ResponseEntity response in postman
Header Response in Postman



6. Response HTTP status as CREATED (201): Here just set a custom status code as CREATED 201
  • Used method - ResponseEntity.status(HttpStatus.CREATED).body();

HTTP Status code as CREATED 201

ResponseEntity response in postman


As you have seen, We have described all the way to HTTP response with ResponseEntity's method. The most important to focus on here are the response custom HTTP status code, custom header, dynamic response based on application logic, and uses of BodyBuilder interface to achieve this.



Post a Comment

0 Comments