Bird
0
0

How can you modify a @RestController method to return HTTP status 201 Created along with the response body?

hard📝 Application Q9 of 15
Spring Boot - REST Controllers
How can you modify a @RestController method to return HTTP status 201 Created along with the response body?
AChange method return type to void and set status manually
BAdd @ResponseStatus(201) annotation on the method
CReturn a ResponseEntity with body and status 201
DUse @PostMapping instead of @GetMapping
Step-by-Step Solution
Solution:
  1. Step 1: Understand how to set HTTP status

    Using ResponseEntity allows setting both body and status code explicitly.
  2. Step 2: Compare alternatives

    @ResponseStatus sets status but does not allow dynamic body easily; void return loses body; changing mapping does not affect status.
  3. Final Answer:

    Return a ResponseEntity with body and status 201 -> Option C
  4. Quick Check:

    ResponseEntity sets status and body [OK]
Quick Trick: Use ResponseEntity to set status and body [OK]
Common Mistakes:
  • Using @ResponseStatus without body
  • Returning void loses response content
  • Confusing HTTP method with status code

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes