Bird
0
0

What is wrong with this controller code?

medium📝 Debug Q14 of 15
Spring Boot - REST Controllers
What is wrong with this controller code?
@RequestMapping("api")
public class ProductController {
  @RequestMapping("/list")
  public String listProducts() { return "products"; }
}
AClass must be annotated with @RestController or @Controller.
BMissing leading slash in class-level @RequestMapping path.
CMethod-level @RequestMapping cannot have a path.
DMethod must return ResponseEntity, not String.
Step-by-Step Solution
Solution:
  1. Step 1: Identify missing controller annotation

    The class is missing @Controller or @RestController. Without it, the class is not registered as a handler despite having @RequestMapping.
  2. Step 2: Validate other options

    Method-level paths are allowed. Leading slash is optional (Spring prepends it). Returning String is valid for view names or with @ResponseBody.
  3. Final Answer:

    Class must be annotated with @RestController or @Controller. -> Option A
  4. Quick Check:

    Controller annotation required [OK]
Quick Trick: Use @RestController or @Controller on controller classes [OK]
Common Mistakes:
  • Missing @RestController or @Controller annotation
  • Thinking method paths are disallowed
  • Confusing @Controller with @RestController requirement

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes