Bird
0
0

What will happen if you annotate a class with @RestController but forget to add @GetMapping or any request mapping on its methods?

medium📝 component behavior Q5 of 15
Spring Boot - REST Controllers
What will happen if you annotate a class with @RestController but forget to add @GetMapping or any request mapping on its methods?
AAll methods will respond to GET requests by default
BThe controller will not handle any HTTP requests
CThe application will fail to start
DThe methods will respond to POST requests by default
Step-by-Step Solution
Solution:
  1. Step 1: Understand mapping annotations role

    Request mapping annotations like @GetMapping define which HTTP requests a method handles.
  2. Step 2: Effect of missing mappings

    Without any mapping, methods are not linked to any URL or HTTP method, so they won't handle requests.
  3. Final Answer:

    The controller will not handle any HTTP requests -> Option B
  4. Quick Check:

    No mapping = no request handling [OK]
Quick Trick: Methods need mapping annotations to handle requests [OK]
Common Mistakes:
  • Assuming default GET mapping
  • Thinking app startup fails
  • Assuming default POST mapping

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes