How to Use ResponseEntity in Spring Boot for HTTP Responses
In Spring Boot,
ResponseEntity is used to represent the entire HTTP response, including status code, headers, and body. You create it by returning ResponseEntity from your controller methods to customize the HTTP response sent to clients.Syntax
The basic syntax of ResponseEntity involves specifying the response body and optionally the HTTP status and headers.
- ResponseEntity<T>: Generic type where
Tis the response body type. - body(T body): Sets the response content.
- status(HttpStatus status): Sets the HTTP status code.
- headers(HttpHeaders headers): Sets HTTP headers.
java
return ResponseEntity.status(HttpStatus.OK).body("Hello World");
Example
This example shows a simple Spring Boot REST controller that returns a ResponseEntity with a JSON body, custom header, and HTTP status 200 OK.
java
import org.springframework.http.HttpHeaders; 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 GreetingController { @GetMapping("/greet") public ResponseEntity<String> greet() { HttpHeaders headers = new HttpHeaders(); headers.add("Custom-Header", "MyValue"); return ResponseEntity .status(HttpStatus.OK) .headers(headers) .body("{\"message\": \"Hello, Spring Boot!\"}"); } }
Output
HTTP/1.1 200 OK
Custom-Header: MyValue
Content-Type: application/json
{"message": "Hello, Spring Boot!"}
Common Pitfalls
Common mistakes when using ResponseEntity include:
- Returning just the body type instead of
ResponseEntitywhen you want to customize status or headers. - Not setting the correct HTTP status code, which can confuse API clients.
- Forgetting to set content type headers when returning JSON or other formats.
Always use ResponseEntity when you need full control over the HTTP response.
java
/* Wrong: returns only body, no status control */ @GetMapping("/wrong") public String wrongResponse() { return "Hello"; } /* Right: returns ResponseEntity with status and body */ @GetMapping("/right") public ResponseEntity<String> rightResponse() { return ResponseEntity.status(HttpStatus.CREATED).body("Hello"); }
Quick Reference
| Usage | Description |
|---|---|
| ResponseEntity.ok(body) | Returns 200 OK with the given body |
| ResponseEntity.status(HttpStatus.CREATED).body(body) | Returns 201 Created with body |
| ResponseEntity.status(HttpStatus.NOT_FOUND).build() | Returns 404 Not Found with no body |
| ResponseEntity.headers(HttpHeaders).body(body) | Returns response with custom headers and body |
| ResponseEntity.badRequest().body(errorMessage) | Returns 400 Bad Request with error message |
Key Takeaways
Use ResponseEntity to control HTTP status, headers, and body in Spring Boot responses.
Return ResponseEntity from controller methods to customize API responses.
Use static methods like ok(), status(), badRequest() for common HTTP statuses.
Always set appropriate HTTP status codes to communicate response meaning.
Include headers when you need to send metadata or control caching.