In Spring Boot, which annotation is used to specify the HTTP status code for a controller method's response?
easy📝 Conceptual Q1 of 15
Spring Boot - Request and Response Handling
In Spring Boot, which annotation is used to specify the HTTP status code for a controller method's response?
A@ResponseStatus
B@RequestMapping
C@GetMapping
D@Controller
Step-by-Step Solution
Solution:
Step 1: Understand the purpose of @ResponseStatus
The @ResponseStatus annotation allows you to define the HTTP status code that a controller method should return.
Step 2: Differentiate from other annotations
@RequestMapping and @GetMapping define the URL and HTTP method, while @Controller marks a class as a controller. Only @ResponseStatus sets the status code.
Final Answer:
@ResponseStatus -> Option A
Quick Check:
Annotation for HTTP status = @ResponseStatus [OK]
Quick Trick:Use @ResponseStatus to set HTTP status codes easily [OK]
Common Mistakes:
Confusing @RequestMapping with status code setting
Using @Controller to set status code
Assuming @GetMapping sets status code
Master "Request and Response Handling" in Spring Boot
9 interactive learning modes - each teaches the same concept differently