【发布时间】:2019-08-06 20:33:36
【问题描述】:
我正在尝试测试两个 Rest API Endpoints ,一个是 get,另一个是 post。这个想法是get响应返回的响应正文应该是帖子的正文。
我正在使用的代码:
RestAssured.baseURI = ROOT_URI;
RequestSpecification httpRequest =
RestAssured.given().header("Authorization", "Basic " + encodedString);
String endPoint="/v1/getworkers";
Response response = httpRequest.request(Method.GET, endPoint);
String responseBody = response.getBody().asString();
现在我得到的响应正文是:
{"workerDetails":
[
{"workerId":"TEST123456",
"securityId":"TESTWORKERID",
"workerStatus":"Active",
"firstName":"Test",
"lastName":"Userone"}
]
}
现在,我发出 post 请求并将 String responseBody 作为正文:
RestAssured.baseURI = ROOT_URI;
String endpoint="/v1/PostWorkers";
RequestSpecification httpRequest =
RestAssured.given().header("Authorization", "Basic " + encodedString)
.header("Content-Type", "application/json").body(postbody);
Response response = httpRequest.request(Method.POST,endpoint);
String responseBody = response.getBody().asString();
我收到错误 400:
timestamp":1552670194453,"status":400,"error":"Bad 请求","异常":"org.springframework.http.converter.HttpMessageNotReadableException","消息":"JSON 解析错误:无法反序列化 java.util.ArrayList 的实例 START_OBJECT 令牌;嵌套异常是 com.fasterxml.jackson.databind.JsonMappingException:不能 从 START_OBJECT 中反序列化 java.util.ArrayList 的实例 令牌\n 在 [来源:java.io.PushbackInputStream@56bba771
怎么办?
【问题讨论】:
-
您是否尝试过此处描述的解决方案? stackoverflow.com/a/39043513/2387977
-
您要反序列化的 DTO 是什么?
标签: java automated-tests rest-assured