使用RestAssured提交嵌套JSONObject遇必填字段报错的解决方法
Hey there! Let's troubleshoot this nested JSON issue with RestAssured. Since your non-nested API requests work fine, the problem almost certainly lies in how you're constructing that nested another.child structure. Here are the most common fixes and best practices to get this working:
1. Double-Check Field Names & Nesting Structure
First, rule out the simplest mistake: typos in field names or incorrect nesting. Make sure you're actually placing the child field inside the another object, not at the root level of your request body.
For example, this is wrong:
// ❌ child is at root level, not inside another given() .body("{\"child\": \"value\", \"parentField\": \"test\"}") .post("/endpoint");
This is correct:
// ✅ child is nested inside another given() .body("{\"another\": {\"child\": \"requiredValue\"}, \"parentField\": \"test\"}") .post("/endpoint");
2. Use Nested Maps for Cleaner Construction
String-based JSON is error-prone. Instead, use nested Java Map objects—RestAssured will automatically serialize them into valid JSON:
// Create the nested child object Map<String, Object> anotherMap = new HashMap<>(); anotherMap.put("child", "your-required-value"); // Build the root request body Map<String, Object> requestBody = new HashMap<>(); requestBody.put("parentField", "some-value"); requestBody.put("another", anotherMap); // Send the request given() .contentType(ContentType.JSON) // Critical to set this! .body(requestBody) .when() .post("/your-api-endpoint") .then() .statusCode(200);
3. Use JSONObject (org.json) Correctly
If you prefer using org.json.JSONObject, ensure you properly nest the child object inside the parent:
// Create the nested child object JSONObject childObj = new JSONObject(); childObj.put("child", "requiredValue"); // Build the parent object and attach the child JSONObject parentObj = new JSONObject(); parentObj.put("parentField", "test-value"); parentObj.put("another", childObj); // This links the nested object correctly // Send the request given() .contentType(ContentType.JSON) .body(parentObj.toString()) // Convert to string, or pass the JSONObject directly (RestAssured supports it) .when() .post("/your-api-endpoint") .then() .statusCode(200);
4. Use POJOs for Type Safety (Recommended)
For more complex requests, define Plain Old Java Objects (POJOs) to represent your request structure. This eliminates manual JSON construction errors entirely:
// Nested POJO for the "another" object class Another { private String child; // Constructor, getters, and setters public Another(String child) { this.child = child; } public String getChild() { return child; } public void setChild(String child) { this.child = child; } } // Root POJO for the entire request body class ApiRequest { private String parentField; private Another another; // Constructor, getters, and setters public ApiRequest(String parentField, Another another) { this.parentField = parentField; this.another = another; } public String getParentField() { return parentField; } public void setParentField(String parentField) { this.parentField = parentField; } public Another getAnother() { return another; } public void setAnother(Another another) { this.another = another; } } // Use the POJOs to build the request ApiRequest request = new ApiRequest("sample-value", new Another("required-child-value")); given() .contentType(ContentType.JSON) .body(request) // RestAssured uses Jackson/Gson to serialize POJOs to JSON .when() .post("/your-api-endpoint") .then() .statusCode(200);
Key Things to Verify
- Always set
contentType(ContentType.JSON)—without this, RestAssured might not serialize nested structures correctly. - Use a tool like Postman to test the raw JSON request first, to confirm the API accepts the structure before coding it in RestAssured.
- Check for case sensitivity—some APIs require exact field name casing (e.g.,
Childvschild).
内容的提问来源于stack exchange,提问作者M G

