nerdexam
Broadcom-VMware

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…

REST

Question

Refer to the exhibit. How can a response status code be set for No Content (204)? (Choose the best answer.)

Exhibit

2V0-72.22PSE question #15 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)
  • A
    2% (1)
  • B
    91% (53)
  • C
    5% (3)
  • D
    2% (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 @PutMapping only 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 - ResponseEntity is a return type, not an annotation.
  • D is wrong because void is perfectly valid when paired with @ResponseStatus; ResponseEntity is 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

#HTTP Status Codes#@ResponseStatus#REST handlers#Spring annotations

Community Discussion

No community discussion yet for this question.

Full 2V0-72.22PSE Practice