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.
3 points to say:
ResponseEntitycontrols status, headers, body; returning the object directly is always 200.- Fixed status:
@ResponseStatus; outcome-dependent (201 +Location, 204,ETag):ResponseEntity. - 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.