ResponseEntity vs @ResponseStatus — Dynamic vs Static Status Codes
Fast recall
Quick revision
Core answer
Understanding when to use declarative annotations versus imperative response building in Spring MVC
Using ResponseEntity everywhere even when the status never changes—@ResponseStatus is cleaner and less code for fixed-status endpoints
Give concrete examples: @ResponseStatus for DELETE returning 204 always; ResponseEntity for GET that returns 200 with data or 404 when resource missing
Interview-ready guide
Interview answer
A lookup method can return ResponseEntity.ok(dto) when found and ResponseEntity.notFound().build() when absent. A create method whose successful outcome is always 201 can use @ResponseStatus(CREATED), although ResponseEntity.created(location).body(dto) also supplies Location. Annotating a NotFoundException with @ResponseStatus is simple but couples that exception to HTTP; @RestControllerAdvice can keep domain and web concerns separate.
ResponseEntity vs @ResponseStatus — Dynamic vs Static Status Codes
Direct Answer
ResponseEntity is a return value containing status, headers, and an optional body, so a method can construct different responses at runtime. @ResponseStatus is runtime metadata that assigns a fixed status (and optional reason) to a handler method or exception type. It is not a compile-time mechanism. For centralized APIs, exception handlers often give more control than annotating exception classes.
Code Example
// @ResponseStatus — FIXED status, declared at compile time
@ResponseStatus(HttpStatus.CREATED) // always 201
@PostMapping("/users")
public User create(@RequestBody User user) {
return userService.save(user);
}
// ResponseEntity — DYNAMIC status decided at runtime
@GetMapping("/users/{id}")
public ResponseEntity<User> getUser(@PathVariable Long id) {
return userService.findById(id)
.map(ResponseEntity::ok) // 200 + body
.orElse(ResponseEntity.notFound().build()); // 404
}
Use @ResponseStatus when the code never changes; ResponseEntity when status, headers, or body vary per request.
Common Mistakes & Pitfalls
- Overusing ResponseEntity for Fixed Status: Freshers wrap every response in ResponseEntity even when the status never changes. A method that always returns 204 doesn't need ResponseEntity.noContent().build()—just annotate with @ResponseStatus(NO_CONTENT) and return void. Simpler code, same result.
- Forgetting @ResponseStatus on Custom Exceptions: You create ResourceNotFoundException but don't annotate it, so Spring returns 500 Internal Server Error instead of 404. The annotation tells Spring what status to send when that exception bubbles up.
- Mixing Both on Same Method: Putting @ResponseStatus(CREATED) on a method that returns ResponseEntity.ok(...) creates confusion—the ResponseEntity status wins, but the annotation misleads readers.
Check your understanding
Answer these without looking back at the article.
- Can you use @ResponseStatus and ResponseEntity on the same method?
- When would you use @ResponseStatus on an exception class?
- How do you return 201 Created with a Location header using @ResponseStatus?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.