簡體   English   中英

使用放心 put 方法測試 api 時出現錯誤請求 400 和反序列化錯誤

[英]Bad Request 400 and deserialization error while testing api using rest assured put method

我正在使用路徑 para 和 query 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());

}

在此處輸入圖片說明 在此處輸入圖片說明

正如您在那里的評論中所分享的那樣,端點需要一個對象列表,而不是您發送的單個對象......只需嘗試用列表包裝它,您就會得到超過 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);

請幫助我們很抱歉! 我們嘗試處理您的請求時發生錯誤。 請放心,我們已經在解決這個問題,並希望很快解決它。

錯誤摘要 400 Bad Request Request Details trans_arb=07b1ec96-4414-49d5-b0c0-e9f3e809e1ae

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM