2V0-72.22PSE · Question #15
Refer to the exhibit. How can a response status code be set for No Content (204)? (Choose the best answer.)
The correct answer is B. Annotate the update() handler method with @ResponseStatus(HttpStatus.NO_CONTENT). @ResponseStatus(HttpStatus.NO_CONTENT) is the correct Spring MVC annotation for declaratively setting a handler method's response code - it tells the framework to return HTTP 204 when the method completes successfully, even when the return type is void. Why the distractors are…
Question
Refer to the exhibit. How can a response status code be set for No Content (204)? (Choose the best answer.)
Exhibit
Options
- AAnnotate the update() handler method with @PutMapping("/store/orders/{id"}",
- BAnnotate the update() handler method with @ResponseStatus(HttpStatus.NO_CONTENT).
- CAnnotate the update() handler method with @ResponseEntity(204).
- DThe update() handler method cannot return a void type, it must return a ResponseEntity type.
How the community answered
(58 responses)- A2% (1)
- B91% (53)
- C5% (3)
- D2% (1)
Explanation
@ResponseStatus(HttpStatus.NO_CONTENT) is the correct Spring MVC annotation for declaratively setting a handler method's response code - it tells the framework to return HTTP 204 when the method completes successfully, even when the return type is void.
Why the distractors are wrong:
- A is wrong because
@PutMappingonly maps the URL and HTTP method; it has no parameter for setting a response status code. - C is wrong because
@ResponseEntity(204)is not a valid annotation -ResponseEntityis a return type, not an annotation. - D is wrong because
voidis perfectly valid when paired with@ResponseStatus;ResponseEntityis an alternative approach, not a requirement.
Memory tip: Think of @ResponseStatus as a sticky note you put on the method to label what status code it returns - whenever you need to override Spring's default 200 OK, reach for @ResponseStatus(HttpStatus.XXX).
Topics
Community Discussion
No community discussion yet for this question.
