JSON 序列化问题:无效定义异常:



我有一个场景,我需要执行一个负面的测试场景。

这是一个用于更新客户对象的 PUT 调用。

我需要将对象转换为 JSON,并且在应用于有效负载之前删除了一个必需属性。

现在,当我提供缺少属性的有效负载时,我期待 400 响应和错误消息。

我使用了以下代码,但有无效定义异常

错误

com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class org.json.JSONObject and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) 

法典:

UpdateCustomerModel updateCustomerModel = new UpdateCustomerModel()
updateCustomerModel.setFirstName("abc");
updateCustomerModel.setLastName("abc");
updateCustomerModel.setEmail("a@a.com");
ObjectMapper mapper = new ObjectMapper();
String json = mapper.writeValueAsString(updateCustomerModel);
JSONObject updateCustomerJson = new JSONObject(json);
updateCustomerJson.getJSONObject("customer").remove("email");
Response response =
given().filter(new RequestLoggingFilter(this.requestCapture))
.header("Authorization", getSession().getToken()).body(updateCustomerJson)
.body(updateCustomerJson)
.when()
.put(Resource.updateCustomer)
.then()
.extract().response();

有效载荷:有效载荷中的必需属性是;

firstname, lastname and email
{
"customer": {
"id": null,
"firstname": "Chuck",
"lastname": "Patterson",
"store_id": null,
"website_id": 1,
"addresses": [
],
"default_billing": null,
"default_shipping": null
}
}

尝试了研发许多配置,但没有乐趣

例如

mapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS(;

有人可以指出任何方向吗?

杰克逊似乎无法序列化您尝试作为正文传入的JSONObject。删除不需要的字段后,为什么不尝试将其转换回字符串?

最新更新