Bird
0
0

What is wrong with this Spring Boot controller class?

medium📝 Debug Q7 of 15
Spring Boot - Spring Annotations
What is wrong with this Spring Boot controller class?
@Controller
public class MyController {
    @GetMapping("/data")
    public String getData() {
        return "data";
    }
}
AMethod getData should be private
BURL mapping is incorrect
CClass should be annotated with @Service
DMissing @ResponseBody annotation to return plain text
Step-by-Step Solution
Solution:
  1. Step 1: Understand @Controller behavior

    @Controller methods by default return view names, not response bodies.
  2. Step 2: Fix to return plain text

    Adding @ResponseBody tells Spring to send the returned string as HTTP response body.
  3. Final Answer:

    Missing @ResponseBody annotation to return plain text -> Option D
  4. Quick Check:

    Return plain text requires @ResponseBody [OK]
Quick Trick: Add @ResponseBody to return raw data from controller methods [OK]
Common Mistakes:
  • Assuming @Controller returns text by default
  • Making method private
  • Wrong annotation on class

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes