Bird
Raised Fist0
Spring Bootframework~10 mins

Message serialization in Spring Boot - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to annotate a class for JSON serialization in Spring Boot.

Spring Boot
public class User {
    private String name;
    private int age;

    // getters and setters

    @[1]
    public String getName() {
        return name;
    }
}
Drag options to blanks, or click blank then click option'
AJsonProperty
BAutowired
CRequestMapping
DComponent
Attempts:
3 left
💡 Hint
Common Mistakes
Using @Autowired instead of @JsonProperty
Confusing Spring annotations with Jackson annotations
2fill in blank
medium

Complete the code to convert a Java object to JSON string using Jackson's ObjectMapper.

Spring Boot
ObjectMapper mapper = new ObjectMapper();
String json = mapper.[1](user);
Drag options to blanks, or click blank then click option'
AwriteValueAsString
BreadValue
CconvertValue
DreadTree
Attempts:
3 left
💡 Hint
Common Mistakes
Using readValue which is for deserialization
Using readTree which returns a JsonNode
3fill in blank
hard

Fix the error in the code to deserialize JSON string to a Java object.

Spring Boot
User user = mapper.[1](json, User.class);
Drag options to blanks, or click blank then click option'
AwriteValueAsString
BwriteValue
CreadValue
DconvertValue
Attempts:
3 left
💡 Hint
Common Mistakes
Using writeValueAsString which is for serialization
Using convertValue incorrectly without JSON string
4fill in blank
hard

Fill both blanks to create a REST controller method that returns JSON response.

Spring Boot
@RestController
public class UserController {

    @GetMapping("/user")
    public [1] getUser() {
        User user = new User("Alice", 30);
        return user;
    }

    public void setUser(@[2] User user) {
        // setter
    }
}
Drag options to blanks, or click blank then click option'
AUser
Bvoid
CJsonProperty
DRequestBody
Attempts:
3 left
💡 Hint
Common Mistakes
Using void as return type for JSON response
Missing @RequestBody on setter parameter
5fill in blank
hard

Fill all three blanks to create a Map with filtered entries serialized to JSON.

Spring Boot
Map<String, Integer> result = Map.of("a", 1, "b", 2, "c", 3);
Map<String, Integer> filtered = result.entrySet().stream()
    .filter(e -> e.getValue() [1] 1)
    .collect(Collectors.[2](
        e -> e.getKey().[3](),
        e -> e.getValue()
    ));
Drag options to blanks, or click blank then click option'
A>
BtoMap
CtoUpperCase
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong comparison operator in filter
Using wrong collector method
Not converting keys to uppercase

Practice

(1/5)
1. What is the main purpose of message serialization in Spring Boot?
easy
A. To handle user authentication and authorization
B. To compile Java code into bytecode
C. To convert objects into a format like JSON for easy data sharing
D. To manage database connections automatically

Solution

  1. Step 1: Understand serialization concept

    Serialization means converting an object into a format that can be sent or stored, like JSON.
  2. Step 2: Relate to Spring Boot usage

    Spring Boot uses serialization to send Java objects as JSON in REST APIs.
  3. Final Answer:

    To convert objects into a format like JSON for easy data sharing -> Option C
  4. Quick Check:

    Serialization = Convert objects to JSON [OK]
Hint: Serialization means turning objects into JSON or similar formats [OK]
Common Mistakes:
  • Confusing serialization with compiling code
  • Thinking serialization manages databases
  • Mixing serialization with security features
2. Which of the following is the correct way to ensure a Java class is serializable by Spring Boot's default JSON serializer?
easy
A. Use @Entity annotation on the class
B. Make the class implement Serializable interface
C. Mark all fields as transient
D. Add public getters for all fields and a no-argument constructor

Solution

  1. Step 1: Identify Spring Boot serialization requirements

    Spring Boot uses Jackson which requires public getters and a no-argument constructor to serialize objects properly.
  2. Step 2: Evaluate options

    Implementing Serializable is not required for JSON serialization. Marking fields transient prevents serialization. @Entity is for database mapping, not serialization.
  3. Final Answer:

    Add public getters for all fields and a no-argument constructor -> Option D
  4. Quick Check:

    Getters + no-arg constructor = JSON serialization [OK]
Hint: Public getters and no-arg constructor enable JSON serialization [OK]
Common Mistakes:
  • Thinking Serializable interface is needed for JSON
  • Marking fields transient disables serialization
  • Confusing @Entity with serialization setup
3. 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);
}
medium
A. ["Alice",30]
B. {"name":"Alice","age":30}
C. {"User":{"name":"Alice","age":30}}
D. Error: Records cannot be serialized

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]
Hint: 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
4. Consider this Spring Boot REST controller snippet:
@GetMapping("/product")
public Product getProduct() {
    return new Product("Book", null);
}

public class Product {
    private String name;
    private String description;

    public Product(String name, String description) {
        this.name = name;
        this.description = description;
    }

    public String getName() { return name; }
    // Missing getter for description
}
What issue will occur when this endpoint is called?
medium
A. The JSON will include only the name field; description is missing
B. The JSON will include both name and description fields
C. A runtime error occurs due to missing getter
D. The endpoint will not compile

Solution

  1. Step 1: Check serialization requirements

    Jackson serializes fields using public getters. Missing getter means that field is ignored in JSON output.
  2. Step 2: Analyze given code

    Product class lacks getter for 'description', so JSON will only show 'name'. No runtime error or compile error occurs.
  3. Final Answer:

    The JSON will include only the name field; description is missing -> Option A
  4. Quick Check:

    Missing getter = field omitted in JSON [OK]
Hint: Missing getter means field won't appear in JSON output [OK]
Common Mistakes:
  • Expecting runtime or compile error
  • Assuming all fields serialize without getters
  • Thinking null values cause errors
5. You want to customize JSON serialization in Spring Boot to exclude fields with null values from the output. Which approach is correct?
hard
A. Configure Jackson's ObjectMapper to set SerializationInclusion to NON_NULL
B. Mark all nullable fields as transient
C. Remove getters for nullable fields
D. Use @JsonIgnore on all fields

Solution

  1. Step 1: Understand how to exclude null fields in JSON

    Jackson allows configuration to exclude null fields by setting SerializationInclusion to NON_NULL.
  2. Step 2: Evaluate options

    Marking fields transient or removing getters prevents serialization entirely, not just null exclusion. @JsonIgnore excludes fields always.
  3. Final Answer:

    Configure Jackson's ObjectMapper to set SerializationInclusion to NON_NULL -> Option A
  4. Quick Check:

    Set SerializationInclusion = NON_NULL to skip nulls [OK]
Hint: Set ObjectMapper to exclude nulls with NON_NULL inclusion [OK]
Common Mistakes:
  • Using transient to exclude nulls (excludes always)
  • Removing getters breaks serialization
  • Using @JsonIgnore excludes fields always
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard