Bird
0
0

What is wrong with this REST controller method?

medium📝 Debug Q7 of 15
Spring Boot - REST Controllers
What is wrong with this REST controller method?
@RestController
public class TestController {
  @GetMapping("/test")
  public String test() {
    System.out.println("Test called");
  }
}
AMethod does not return a value but has String return type
BMissing @PostMapping annotation
CClass should be annotated with @Controller instead
DMethod should be static
Step-by-Step Solution
Solution:
  1. Step 1: Check method return type and body

    The method declares a return type of String but does not return anything.
  2. Step 2: Understand Java method requirements

    All non-void methods must return a value; missing return causes compile error.
  3. Final Answer:

    Method does not return a value but has String return type -> Option A
  4. Quick Check:

    Non-void methods must return a value [OK]
Quick Trick: Non-void methods must return a value [OK]
Common Mistakes:
  • Assuming missing @PostMapping is error
  • Confusing @RestController with @Controller
  • Thinking method must be static

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes