java - 使用放心 put 方法测试 api 时出现错误请求 400 和反序列化错误

标签 java rest rest-assured web-api-testing rest-assured-jsonpath

我正在使用路径 para 和查询 para 测试端点,并使用一些参数更新请求。当我发送请求内容类型是json 并且请求对于我试图更改的数据看起来不错,但是当收到响应时

内容类型为文本 我收到 400 错误错误请求作为状态代码 和错误消息

javax.ws.rs.ProcessingException:RESTEASY008200:JSON 绑定(bind)反序列化错误:javax.json.bind.JsonbException:无法推断用于解码的类型:java.util.List

我的代码

@Given("^api is set up with required details$")
public void api_is_set_up_with_user_data() throws Throwable {
    loadProp();
    base_url = prop.getProperty("baseurl");
    RestAssured.baseURI = base_url;

}

@When("^a user retrieves the preferences by userid \"([^\"]*)\" and type \"([^\"]*)\"$")
public void a_user_retrieves_the_preferences_by_userid_and_type(String userid, String parameter) throws Throwable {
    request = given().pathParam("user_id", userid).queryParam("type", parameter);
    System.out.println(request);
    ENDPOINT_GET_USER_BY_ID = base_url + "{user_id}/preferences";
    response = request.when().get(ENDPOINT_GET_USER_BY_ID);
    System.out.println("response: " + response.prettyPrint());
}

@Then("^updates the value \"([^\"]*)\" of name \"([^\"]*)\"$")
public void updates_the_value_of_name(String value, String displaytext) throws Throwable {
    HashMap<String,String> post = new HashMap<String,String>();
    post.put("displaytext",displaytext);
    post.put("value",value);
    response = request.contentType("application/json").accept("*/*").body(post).put(ENDPOINT_GET_USER_BY_ID);
//        response = request.header("Content-Type", "application/json").body(post).put(ENDPOINT_GET_USER_BY_ID);

    System.out.println("Response : " + response.asString());
    System.out.println("Statuscode : " +response.getStatusCode());

}

enter image description here enter image description here

最佳答案

正如您在评论中所分享的那样,端点需要一个对象列表,而不是您发送的单个对象...只需尝试用列表包装它,您就会收到 400 错误。

您要发送的内容;

{
    "displayText": "Warrants", 
    "value": "true"  // I don't know about this value field here
}

正如您所分享的,预期是什么;

[ 
    {
        "displayText": "", 
        "preferences": [ { "category": "", "displaytext": "", } ], 
        "priority": "20" 
    }
] 

一个问题是您必须在列表中发送对象,同时将对象作为映射传递也有些适得其反,最好使用 RQ 中使用的相同对象。

public class Request {

    private String displayText;
    private List<Preference> preferences;
    private Integer priority;

    //getter, setter,etc
}

&在放心测试中使用它;

List<Request> requestList = new ArrayList<>();
Request request = new Request();
request.setDisplayText("etc");
... // set other stuff
requestList.add(request);
response = request.contentType("application/json").accept("*/*").body(requestList).put(ENDPOINT_GET_USER_BY_ID);

关于java - 使用放心 put 方法测试 api 时出现错误请求 400 和反序列化错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56799752/

相关文章:

java - 如何从 jsonarray 检查特定的 json 数组参数值

java - 如何区分java计算器中的第一个int和第二个int?

java - 在Java中测试@键是否被按下

java - 通过 JQuery 调用外部域上的 RESTful 服务

java - Spring REST API 中的 Json 模式验证

xml - java.lang.NoSuchMethodError : org. springframework.util.ClassUtils.forName(Ljava

java - 放心 : unable to parse response for required value using Xpath

java - 如何使用 postman 或API从 "Download"文件夹下载文件

java - 需要使用 forEach 循环两次并具有唯一的 id

java - 尝试从 serenity.conf 读取环境特定数据时获取空值