请放心:无法反序列化 json 对其 POJO 的响应

Rest Assured: Fail to deserialize json response to its POJO

我从 Azure 登录 REST 调用中返回了以下 JSON:

{
    "token_type": "Bearer",
    "expires_in": "3600",
    "ext_expires_in": "3600",
    "expires_on": "1560857196",
    "not_before": "1560853296",
    "resource": "https://management.azure.com",
    "access_token": "d9f9s..." //I cut the value significantly here...
}

我为其搭建的POJO如下:

public class AzureLoginResponse {

    private String tokenType;
    private String expiresIn;
    private String extExpiresIn;
    private String expiresOn;
    private String notBefore;
    private String resource;
    private String accessToken; 

    //setters\getters

}

当我这样做的时候:

   @Given("^Azure Login Request Executed$")
    public void azureLoginExecuted() {

        RestAssured.baseURI = BASE_URI;

        Response response =
        given()  //Add x-www-form-urlencoded body params:
            .formParam(GRANT_TYPE_KEY, GRANT_TYPE_VALUE)
            .formParam(AUTO_TEAM_CLIENT_ID_KEY, AUTO_TEAM_CLIENT_ID_VALUE)
            .formParam(AUTO_TEAM_CLIENT_SECRET_KEY, AUTO_TEAM_CLIENT_SECRET_VALUE)
            .formParam(RESOURCE_KEY, RESOURCE_VALUE)
        .when()
            .post(AUTO_TEAM_TENANT_ID + RESOURCE); //Send the request along with the resource

        AzureLoginResponse azureLoginResponse = response.as(AzureLoginResponse.class);

    }

azureLoginResponse 在其中获取以下数据:

azureLoginResponse = {AzureLoginResponse@3532} 
 tokenType = null
 expiresIn = null
 extExpiresIn = null
 expiresOn = null
 notBefore = null
 resource = "https://management.azure.com"
 accessToken = null

所以,只有 'resource' 属性 被填充,而像

这样的测试
assertThat(expires_in_val, greaterThan(min_expected_expires_in_val));

response.then().body("resource", equalTo(expected_resource));

顺利通过。

您的 POJO 键与响应中的键不匹配,因此为空值。如您所见,响应中出现的键是 snake_case 而您的 POJO 中使用的键是驼峰式。