Bird
0
0

What is wrong with this controller code?

medium📝 Debug Q7 of 15
Spring Boot - REST Controllers
What is wrong with this controller code?
 @RestController
 @RequestMapping("/api")
 public class DemoController {
   @RequestMapping
   public String home() { return "Home"; }
 }
AClass-level @RequestMapping should specify HTTP method
BController class missing @Component annotation
CMethod must have @GetMapping instead of @RequestMapping
DMethod-level @RequestMapping missing path, defaults to base path
Step-by-Step Solution
Solution:
  1. Step 1: Analyze method-level @RequestMapping without path

    When no path is specified, the method maps to the base path defined at class level.
  2. Step 2: Confirm if this is valid

    This is valid and means the method handles requests to "/api".
  3. Final Answer:

    Method-level @RequestMapping missing path, defaults to base path -> Option D
  4. Quick Check:

    Empty method path means base path used [OK]
Quick Trick: Empty method path means base path used [OK]
Common Mistakes:
  • Assuming method must specify path
  • Confusing @RequestMapping with HTTP method annotations
  • Thinking @Component is required on controller

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes