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

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


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


javax.ws.rs.ProcessingException:RESTEASY008200:JSON 绑定反序列化错误: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());


}

http://img2.mukewang.com/63f6c7550001ca8805600809.jpg

红颜莎娜
浏览 109回答 1
1回答

不负相思意

正如您在那里的评论中所分享的那样,端点需要一个对象列表,而不是您发送的单个对象......只需尝试用列表包装它,您就会遇到 400 错误。您要发送的内容;{&nbsp; &nbsp; "displayText": "Warrants",&nbsp;&nbsp; &nbsp; "value": "true"&nbsp; // I don't know about this value field here}正如您所分享的那样,预期是什么;[&nbsp;&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; "displayText": "",&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; "preferences": [ { "category": "", "displaytext": "", } ],&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; "priority": "20"&nbsp;&nbsp; &nbsp; }]&nbsp;一个问题是您必须在列表中发送对象,同时将对象作为映射传递也会适得其反,最好使用 RQ 中使用的相同对象。public class Request {&nbsp; &nbsp; private String displayText;&nbsp; &nbsp; private List<Preference> preferences;&nbsp; &nbsp; private Integer priority;&nbsp; &nbsp; //getter, setter,etc}& 在您的身体中放心使用它;List<Request> requestList = new ArrayList<>();Request request = new Request();request.setDisplayText("etc");... // set other stuffrequestList.add(request);response = request.contentType("application/json").accept("*/*").body(requestList).put(ENDPOINT_GET_USER_BY_ID);
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java