Bird
0
0

Identify the error in this Spring Boot REST controller code:

medium📝 Debug Q14 of 15
Spring Boot - REST Controllers
Identify the error in this Spring Boot REST controller code:
@RestController
public class UserController {
    @GetMapping("/user")
    public User getUser() {
        User user = new User();
        user.setName("Alice");
        return user;
    }
}
Assume User is a simple POJO with a name field and getters/setters.
ANo error; code will return User object as JSON
BUser class must be annotated with @Entity
CMissing @ResponseBody annotation on getUser() method
DMissing @RequestMapping on class level
Step-by-Step Solution
Solution:
  1. Step 1: Check @RestController effect

    @RestController automatically adds @ResponseBody to all methods, so no need to add it explicitly.
  2. Step 2: Verify User class requirements

    User is a POJO; it does not need @Entity unless used with database. Returning it will serialize to JSON automatically.
  3. Final Answer:

    No error; code will return User object as JSON -> Option A
  4. Quick Check:

    @RestController auto @ResponseBody = No error; code will return User object as JSON [OK]
Quick Trick: @RestController adds @ResponseBody automatically [OK]
Common Mistakes:
  • Adding unnecessary @ResponseBody on methods
  • Confusing @Entity with serialization
  • Thinking class-level @RequestMapping is mandatory

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes