Bird
0
0

Given the following Spring Boot controller method, what will be the JSON output when accessing /user?

medium📝 component behavior Q13 of 15
Spring Boot - Messaging
Given the following Spring Boot controller method, what will be the JSON output when accessing /user?
public record User(String name, int age) {}
@GetMapping("/user")
public User getUser() {
    return new User("Alice", 30);
}
A["Alice",30]
B{"name":"Alice","age":30}
C{"User":{"name":"Alice","age":30}}
DError: Records cannot be serialized
Step-by-Step Solution
Solution:
  1. Step 1: Understand record serialization in Spring Boot

    Spring Boot with Jackson supports Java records and serializes them as JSON objects with field names.
  2. Step 2: Predict output for User record

    The record fields 'name' and 'age' become JSON keys with their values.
  3. Final Answer:

    {"name":"Alice","age":30} -> Option B
  4. Quick Check:

    Records serialize as JSON objects with fields [OK]
Quick Trick: Java records serialize as JSON objects with field names [OK]
Common Mistakes:
  • Expecting JSON array instead of object
  • Thinking records cause serialization errors
  • Assuming nested JSON with class name

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes