ResponseEntity — Controlling HTTP Status, Headers, and Body
Fast recall
Quick revision
Core answer
Whether the candidate understands when explicit HTTP response control is needed vs when plain return types are sufficient.
Using ResponseEntity everywhere including simple 200 responses where a plain return type is cleaner and more readable.
Mention ResponseEntity.created(URI) for 201 with Location header, and explain how @ResponseStatus can replace ResponseEntity for fixed status codes.
Interview-ready guide
Interview answer
ResponseEntity is the controller return type that gives you explicit control over every dimension of the HTTP response — status code, headers, and body — when @ResponseStatus alone is not sufficient.
Common patterns
ResponseEntity.ok(body)—200 OKwith bodyResponseEntity.created(location).body(dto)—201 CreatedwithLocationheader and bodyResponseEntity.noContent().build()—204 No Contentwith empty bodyResponseEntity.notFound().build()—404 Not Foundwith empty bodyResponseEntity.status(HttpStatus.CONFLICT).body(errorDto)— any custom status with body
When `ResponseEntity` is necessary over `@ResponseStatus`
- The status code depends on runtime logic (created vs existing resource →
201vs200) - Custom response headers are needed (
Location,ETag,X-Request-Id) - The
Locationheader for201 Createdmust point to the new resource URI
Type safety
ResponseEntitycommunicates the exact body type to the compiler and SpringdocResponseEntityexplicitly signals no body — clearer thanResponseEntitywithnull
Build the URI for Location with UriComponentsBuilder.fromCurrentRequest().path("/{id}").buildAndExpand(order.getId()).toUri() — do not hardcode the host or port.
ResponseEntity — Controlling HTTP Status, Headers, and Body
Direct Answer
ResponseEntity gives you explicit control over the HTTP status code, response headers, and body. Returning a plain object always produces 200 OK with default headers. Use ResponseEntity when you need to return 201 Created with a Location header, 204 No Content, conditional responses with ETags, or any non-200 status code.
Common Mistakes & Pitfalls
Using ResponseEntity everywhere including simple 200 responses where a plain return type is cleaner and more readable.
Check your understanding
Answer these without looking back at the article.
- What are the trade-offs when applying ResponseEntity — Controlling HTTP Status, Headers, and Body in production?
- What are the trade-offs when applying ResponseEntity — Controlling HTTP Status, Headers, and Body in production?
- What are the trade-offs when applying ResponseEntity — Controlling HTTP Status, Headers, and Body in production?
Related interview prep categories
1Want a guided curriculum order?
These modules are also sequenced in our flagship backend interview track — open it when you prefer an ordered path instead of browsing by topic.