Bird
0
0

Consider this Spring Boot REST controller method:

medium📝 component behavior Q4 of 15
Spring Boot - Messaging
Consider this Spring Boot REST controller method:
@GetMapping("/employee")
public Employee getEmployee() {
  return new Employee("Bob", 25);
}

Employee class has private fields 'name' and 'age' with public getters. What JSON will be returned?
A{"name":"Bob","age":25}
B{"name":"Bob"}
C{"age":25}
D{}
Step-by-Step Solution
Solution:
  1. Step 1: Identify serialization behavior

    Jackson serializes all fields with public getters by default.
  2. Step 2: Check fields and getters

    Employee has 'name' and 'age' with public getters, so both are included.
  3. Step 3: Determine JSON output

    Both fields appear in JSON with their values.
  4. Final Answer:

    {"name":"Bob","age":25} -> Option A
  5. Quick Check:

    Public getters produce full JSON output [OK]
Quick Trick: Public getters expose fields in JSON output [OK]
Common Mistakes:
  • Assuming private fields are excluded without getters
  • Expecting partial JSON output
  • Forgetting default Jackson behavior

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes