beginnerBuilding REST APIs
Why should a REST controller return `ResponseEntity<T>` instead of just `T`?
Returning the bare object always yields HTTP 200 with the object serialized as the body, giving you no control over status code or headers. `ResponseEntity<T>` lets the same method return 201 Created with a `Location` header after a POST, or 404 with an empty body when a lookup misses — the status code becomes an explicit part of the API contract instead of an accident of always-200.
Ready to master this question?
Generate a complete walkthrough — background, the full answer in plain language, a working code example explained line by line, a real-world scenario, common mistakes, and how this same question gets asked in different ways.
Sign in to generate a response