
How can a response status code be set for No Content (204)? (Choose the best answer.)
Show answerHide answer
Correct answer: B Annotate the update() handler method with @ResponseStatus(HttpStatus.NO_CONTENT).
Explanation
Explanation:In Spring MVC, to set a fixed HTTP status code for a handler method (such as 204 No Content), you use the @ResponseStatus annotation at the method level.
@ResponseStatus(HttpStatus.NO_CONTENT) tells Spring to respond with HTTP 204 when the method completes successfully, regardless of the return type (including void).
204 No Content is commonly used for PUT, POST, or DELETE requests where the server successfully processes the request but has no response body to return.
❌ Why the others are incorrect
A – @PutMapping does not accept a status code as an argument. The @RequestMapping variants (like @PutMapping) only accept path, headers, produces/consumes, etc. Putting HttpStatus.NO_CONTENT there is syntactically invalid.
C – @ResponseEntity is a class, not an annotation. Returning new ResponseEntity<>(HttpStatus.NO_CONTENT) would work, but @ResponseEntity(204) is not valid annotation syntax. The question asks for annotating the method, not returning a specific type.
D – False. A handler method can return void (e.g., when using @ResponseStatus), and Spring still sends the correct status code. Returning ResponseEntity is one option but not required.
📘 Reference
Spring Framework Documentation — @ResponseStatus annotation: "Marks a method or exception class with the status code and reason that should be returned."
HTTP 204: Successful request, no content to send in response body.






