SteelReed

Search

Search posts, questions…

Spring BootQuestion 15 / 26

Junior

What is ResponseEntity? When do you return a ResponseEntity, a plain object, or use @ResponseStatus?

Short answer

ResponseEntity<T> is the whole HTTP response: status, headers, body. Returning an object directly always gives 200 OK; when status or headers depend on the outcome use ResponseEntity, and for a fixed status @ResponseStatus is shorter.

always 200fixed status, 201depends on outcome,headerserrors, 404What does the responseneed?Return the object@ResponseStatusResponseEntityThrow exceptionadvice handles it

3 points to say:

  1. ResponseEntity controls status, headers, body; returning the object directly is always 200.
  2. Fixed status: @ResponseStatus; outcome-dependent (201 + Location, 204, ETag): ResponseEntity.
  3. 404s and errors in general: throw an exception and let the advice return one consistent format.

Keep reading the deep dive

Create a free account to read the full explanation and follow-up questions, mark questions as studied and review them on a schedule.