Bird
0
0

Which of the following is the correct way to declare a Spring Boot REST controller?

easy📝 Syntax Q12 of 15
Spring Boot - REST Controllers
Which of the following is the correct way to declare a Spring Boot REST controller?
A@Service public class MyController {}
B@Controller public class MyController {}
C@RestService public class MyController {}
D@RestController public class MyController {}
Step-by-Step Solution
Solution:
  1. Step 1: Identify the correct annotation for REST controllers

    @RestController is the correct annotation to create REST APIs in Spring Boot.
  2. Step 2: Check other annotations

    @Controller is for MVC controllers returning views, @RestService is not a valid annotation, and @Service is for service classes.
  3. Final Answer:

    @RestController public class MyController {} -> Option D
  4. Quick Check:

    Correct REST controller syntax = @RestController public class MyController {} [OK]
Quick Trick: Use @RestController for REST APIs, not @Controller [OK]
Common Mistakes:
  • Using @Controller instead of @RestController
  • Using non-existent @RestService annotation
  • Confusing @Service with controller annotations

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes