← Back to libraryQuestion 236 of 468
🔌REST AssuredIntermediate

Serialization and Deserialization (POJO to/from JSON)

📌 Definition:

Serialization converts a Java object to a JSON request body; deserialization converts a JSON response back into a Java object (POJO). REST Assured does both automatically using Jackson or Gson on the classpath.

📖 Detailed Explanation:

For serialization, passing a POJO to .body() emits JSON. For deserialization, .extract().as(User.class) or response.as(User[].class) maps the JSON response into typed Java objects, so you assert with normal getters (user.getEmail()) instead of GPath strings. This requires a matching POJO (fields/annotations aligning with JSON keys — @JsonProperty for name mismatches) and a JSON mapper dependency. Deserializing gives compile-time-safe, refactorable assertions and easy reuse of the same model across create/read tests. Missing/mismatched fields or absent Jackson cause deserialization errors.

🔑 Key Points:
  • .body(pojo) serializes; .as(Pojo.class) deserializes
  • Response → typed object → assert with getters (type-safe)
  • POJO fields must map to JSON keys (@JsonProperty for mismatches)
  • Needs Jackson/Gson; mismatches cause mapping errors
🌍 Real-World Example:

User created = post(...).then().extract().as(User.class); assertEquals('QA', created.getName()); — the response becomes a typed object for clean, refactor-safe assertions.

🎯 Scenario-Based Interview Question:

extract().as(User.class) throws an UnrecognizedPropertyException. What are the likely causes and fixes?