Challenge - 5 Problems
Message Serialization Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output of this Spring Boot REST controller method?
Consider a Spring Boot REST controller method that returns a Java object. What will the HTTP response body contain?
Spring Boot
record User(String name, int age) {} @RestController public class UserController { @GetMapping("/user") public User getUser() { return new User("Alice", 30); } }
Attempts:
2 left
💡 Hint
Spring Boot uses Jackson by default to convert Java objects to JSON in REST responses.
✗ Incorrect
Spring Boot automatically serializes Java objects returned by REST controller methods into JSON format using Jackson library, unless configured otherwise.
📝 Syntax
intermediate1:30remaining
Which annotation enables JSON serialization of a Java class in Spring Boot?
You want to ensure a Java class is serialized to JSON correctly in Spring Boot REST responses. Which annotation should you use?
Attempts:
2 left
💡 Hint
This annotation controls which properties are included during serialization.
✗ Incorrect
@JsonInclude controls inclusion of properties during JSON serialization. The other annotations serve different purposes.
🔧 Debug
advanced2:30remaining
Why does this Spring Boot REST method return an empty JSON object?
Given this code, why does the HTTP response body show {} instead of the expected data?
public class Product {
private String name;
private double price;
public Product(String name, double price) {
this.name = name;
this.price = price;
}
}
@RestController
public class ProductController {
@GetMapping("/product")
public Product getProduct() {
return new Product("Book", 12.99);
}
}
Attempts:
2 left
💡 Hint
Jackson needs access to fields via getters or public fields to serialize.
✗ Incorrect
Jackson by default serializes Java objects using getters. Without getters, it serializes an empty object {}.
🧠 Conceptual
advanced2:00remaining
What is the role of the MessageConverter in Spring Boot serialization?
In Spring Boot, what does the HttpMessageConverter do during REST API calls?
Attempts:
2 left
💡 Hint
Think about how data moves between HTTP and Java objects.
✗ Incorrect
HttpMessageConverter converts HTTP request bodies into Java objects and Java objects into HTTP response bodies, enabling serialization and deserialization.
❓ state_output
expert3:00remaining
What is the JSON output of this Spring Boot controller with custom serialization?
Given this code, what JSON will the /order endpoint return?
public class Order {
private int id;
private double amount;
public Order(int id, double amount) {
this.id = id;
this.amount = amount;
}
@JsonProperty("order_id")
public int getId() {
return id;
}
@JsonIgnore
public double getAmount() {
return amount;
}
}
@RestController
public class OrderController {
@GetMapping("/order")
public Order getOrder() {
return new Order(101, 250.75);
}
}
Attempts:
2 left
💡 Hint
Look at the @JsonProperty and @JsonIgnore annotations on getters.
✗ Incorrect
@JsonProperty renames the id field to order_id in JSON. @JsonIgnore hides the amount field from JSON output.