ResponseEntity lets you send back a full HTTP response with status, headers, and body. It gives you full control over what the client receives.
ResponseEntity for full response control in Spring Boot
ResponseEntity<T> response = ResponseEntity.status(HttpStatus.STATUS_CODE)
.header("Header-Name", "value")
.body(bodyObject);You can chain methods like status(), header(), and body() to build the response.
The generic T is the type of the response body.
return ResponseEntity.ok("Success message");
return ResponseEntity.status(HttpStatus.NOT_FOUND).body("Item not found");
return ResponseEntity.status(HttpStatus.CREATED) .header("Location", "/items/123") .body(newItem);
This Spring Boot controller has two endpoints. The first returns HTTP 200 with a custom header and a greeting message. The second returns HTTP 404 with a simple error message.
import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class SampleController { @GetMapping("/greet") public ResponseEntity<String> greetUser() { return ResponseEntity.status(HttpStatus.OK) .header("Custom-Header", "HelloHeader") .body("Hello, user!"); } @GetMapping("/notfound") public ResponseEntity<String> notFound() { return ResponseEntity.status(HttpStatus.NOT_FOUND) .body("Resource not found"); } }
Use ResponseEntity when you need more than just a response body.
Headers can be added multiple times if needed.
Returning just an object without ResponseEntity defaults to HTTP 200.
ResponseEntity lets you control status, headers, and body in HTTP responses.
It is useful for REST APIs needing precise HTTP responses.
You build it by chaining status(), header(), and body() methods.
Practice
ResponseEntity in a Spring Boot REST controller?Solution
Step 1: Understand ResponseEntity role
ResponseEntity is designed to give full control over HTTP response details like status code, headers, and body.Step 2: Compare with other options
Mapping JSON, handling transactions, or configuring properties are unrelated to ResponseEntity's purpose.Final Answer:
To control the HTTP status, headers, and body of the response -> Option AQuick Check:
ResponseEntity controls full HTTP response [OK]
- Confusing ResponseEntity with JSON mapping
- Thinking it manages database operations
- Assuming it configures app settings
ResponseEntity with status 404 and a body message "Not Found"?Solution
Step 1: Check ResponseEntity builder methods
The correct builder pattern is to callstatus()first, thenbody().Step 2: Validate options
return ResponseEntity.status(404).body("Not Found"); usesstatus(404).body("Not Found")correctly. return ResponseEntity.body("Not Found").status(404); reverses the order, which is invalid. return new ResponseEntity<>("Not Found", 404); uses a constructor but 404 should be HttpStatus, not int. return ResponseEntity.create(404, "Not Found"); uses a non-existent method.Final Answer:
return ResponseEntity.status(404).body("Not Found"); -> Option DQuick Check:
Use status().body() chaining [OK]
- Calling body() before status()
- Using integer instead of HttpStatus enum
- Using non-existent ResponseEntity methods
return ResponseEntity.status(HttpStatus.CREATED)
.header("Location", "/items/123")
.body("Item created");What will be the HTTP status and headers in the response?
Solution
Step 1: Analyze status method
The code sets status to HttpStatus.CREATED which is 201.Step 2: Analyze header method
The header method adds a "Location" header with value "/items/123".Final Answer:
Status 201 Created with header Location: /items/123 -> Option BQuick Check:
Status = 201, Header Location set [OK]
- Assuming default 200 OK status
- Forgetting headers are added by header()
- Confusing status codes meanings
@GetMapping("/test")
public ResponseEntity<String> test() {
return ResponseEntity.body("Hello").status(HttpStatus.OK);
}Solution
Step 1: Check method chaining order
ResponseEntity builder requires callingstatus()beforebody(). Here,body()is called first, which is invalid.Step 2: Validate other options
ResponseEntity.body() is a static method returning a builder, no new keyword needed. HttpStatus.OK is valid. Returning ResponseEntity<String> is correct for REST.Final Answer:
The order of method calls is incorrect; status() must come before body() -> Option CQuick Check:
Call status() before body() [OK]
- Calling body() before status()
- Trying to instantiate ResponseEntity with new
- Confusing return types in controller
Solution
Step 1: Use correct builder pattern
ResponseEntity builder requires callingstatus()first, thenheader(), thenbody().Step 2: Validate options
return ResponseEntity.status(HttpStatus.ACCEPTED) .header("X-Request-ID", "abc123") .body(Map.of("message", "Processing")); follows correct chaining and uses Map.of for body. return ResponseEntity.body(Map.of("message", "Processing")) .header("X-Request-ID", "abc123") .status(HttpStatus.ACCEPTED); calls body() first, invalid order. return new ResponseEntity<>(Map.of("message", "Processing"), HttpStatus.ACCEPTED, Map.of("X-Request-ID", "abc123")); uses a constructor with three arguments which does not exist. return ResponseEntity.create(HttpStatus.ACCEPTED, Map.of("message", "Processing")) .header("X-Request-ID", "abc123"); uses a non-existent create() method.Final Answer:
return ResponseEntity.status(HttpStatus.ACCEPTED) .header("X-Request-ID", "abc123") .body(Map.of("message", "Processing")); -> Option AQuick Check:
status() -> header() -> body() chaining [OK]
- Calling body() before status()
- Using non-existent ResponseEntity constructors
- Trying to use create() method which doesn't exist
Ready to prove it?
Pass the test and this pattern moves unknown
