not sure whats going on, the full error is:
不知道发生了什么,全部的错误是:
Problem with i/o No serializer found for class org.json.JSONObject and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS) )
I am trying to send a PUT request to a RESTful service. I am parsing a POST And sending modified key/value pairs for 'ID' and 'enabled' for the PUT.
我正在尝试向RESTful服务发送PUT请求。我正在解析一个帖子,并为“ID”和“enabled”发送修改后的键/值对。
@Test(dependsOnMethods= {"Post"})
public void Put() throws IOException {
logger.logTestActivity("Testing FORM data POST using Excel file");
requestBuilder = new RequestSpecBuilder();
String jsonString = "";
boolean enabledModify = false;
try {
BufferedReader in = new BufferedReader(new FileReader(
xmlTest.getParameter("json-post")));
String str;
while ((str = in.readLine()) != null) {
jsonString += str;
}
JSONObject jsonObjPut = new JSONObject(jsonString);
jsonObjPut.put("_id", createUserId);
jsonObjPut.put("enabled",enabledModify);
System.out.println(jsonObjPut.toString());
in.close();
requestBuilder.setContentType(ContentType.JSON);
requestSpecification = requestBuilder.build();
responseBuilder.expectStatusCode(Integer.parseInt(xmlTest
.getParameter("http-status-code-200")));
responseBuilder.expectContentType(ContentType.JSON);
responseSpecification = responseBuilder.build();
System.out.println(createUserId);
String responseJson = given().body(jsonObjPut).
when().put("/" + createUserId).asString();
System.out.println(responseJson);
logger.logTestActivity("Finished testing FORM data POST using Excel file");
} catch (AssertionError e ) {
logger.logTestActivity(
"Error testing FORM data post: " + e.getMessage(),logger.ERROR);
System.out.println("REST URL: " + RestAssured.baseURI + " "
+ RestAssured.port + " " + RestAssured.basePath );
Assert.fail("Error testing FORM data POST: " + e.getMessage());
throw e;
} catch (IOException | JSONException e) {
System.out.println("Problem with i/o" + e.getMessage());
}
}
createUserID is a global variable that is the ID parsed from the POST.
createUserID是一个全局变量,它是从POST解析的ID。
JSON file that is getting parsed looks like this:
正在解析的JSON文件如下:
{
"enabled" : false,
"_id" : "fdse332a-22432d-4432b"
}
In a before test method I am setting up the restassured with all the appropriate url endpoints...
在测试之前的方法中,我使用所有适当的url端点设置restassure…
Also, the PUT is also failing, with a NULLPointerException Error. That may be another post in the future!
此外,PUT也失败了,出现了NULLPointerException错误。那可能是未来的另一篇文章!
2 个解决方案
#1
10
Solution: I was not converting my JSON object to a string when passing to restassured.
解决方案:在传递给restassure时,我并没有将JSON对象转换为字符串。
String responseJson = given().body(jsonObjPut.toString). did the trick. I now modify my existing json with generated ID and do a successful PUT on the RESTful service.
字符串responseJson =给定().body(jsonObjPut.toString)。起了作用。现在,我使用生成的ID修改现有的json,并在RESTful服务上进行成功的放置。
#2
0
I was also facing same issue with java Spring rest Api. It was throwing BeanSerializerExcption. Use JsonNODE
class instead of JSONObject
.
我还面临着java Spring rest Api的同样问题。这是扔BeanSerializerExcption。使用JsonNODE类代替JSONObject。
#1
10
Solution: I was not converting my JSON object to a string when passing to restassured.
解决方案:在传递给restassure时,我并没有将JSON对象转换为字符串。
String responseJson = given().body(jsonObjPut.toString). did the trick. I now modify my existing json with generated ID and do a successful PUT on the RESTful service.
字符串responseJson =给定().body(jsonObjPut.toString)。起了作用。现在,我使用生成的ID修改现有的json,并在RESTful服务上进行成功的放置。
#2
0
I was also facing same issue with java Spring rest Api. It was throwing BeanSerializerExcption. Use JsonNODE
class instead of JSONObject
.
我还面临着java Spring rest Api的同样问题。这是扔BeanSerializerExcption。使用JsonNODE类代替JSONObject。