我通常能够创建类作为 .body() 的输入,并放心地正确读取所有数据,但不能使用数组。
我尝试将对象类声明为数组,但放心并没有按照我的意愿正确接受它。
使用 Rest-assured 时,我可以将对象数组作为 .body 发送吗?
请求正文
[
{
"product_type" : "1",
"request_by" : "android",
},
{
"product_type" : "2",
"request_by" : "ios",
}
]
我做的类(class)
public class ProdReq {
private String product_type;
private String request_by;
public String getProduct_type() {
return product_type;
}
public void setProduct_type(String product_type) {
this.product_type = product_type;
}
public String getRequest_by() {
return request_by;
}
public void setRequest_by(String request_by) {
this.request_by = request_by;
}
我用来获取响应的代码
ProdReq[] prodReq = new ProdReq[2]
//set the data
......
given().when().body(prodReq).post({{api_url}}).then().extract().response();
我应该创建该类的 JSONObject(如果可能),然后将它们放入 JSONArray 中吗?
最佳答案
@GFB 您设置了 ContentType 吗?尝试使用这样的东西:
List<ProdReq> prodReq = new ArrayList<>();
... set up the data.
given().contentType(ContentType.JSON).when().body(prodReq).post({{api_url}}).then().extract().response();
我在项目中使用对象序列化为 JSON 主体,没有任何问题。
关于java - 对象数组作为安心中的请求主体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42341727/