稍有放心:无法将响应转换为对象数组Re

时间:2019-07-18 12:35:47

标签: java json rest-assured

我有以下POJO类:

public class AdcServerGroupsResponse {

  private Object availabilityGroup;
  private String cloud;
  private Object fqdns;
  private String groupType;
  private String healthCheck;
  private String name;
  private Object scalingGroup;
  private String selectionMetric;
  private List<String> serverAddresses = null;
  private List<Object> serverSubnet = null;

//Getters\Setters
}

描述以下响应json:

[
    {
        "availability_group": null,
        "cloud": "1",
        "fqdns": null,
        "group_type": "discrete",
        "health_check": "http",
        "name": "t1app1",
        "scaling_group": null,
        "selection_metric": "round_robin",
        "server_addresses": [
            "1.1.1.1",
            "1.1.1.2"
        ],
        "server_subnet": null
    }
]

这是我尝试使用响应值进行验证的方法:

public void serverAddressesDoesntContain(String wrong_ip, String username) {

    response = UserDataMap.SINGLETON.getResponsePerUser(username);
    AdcServerGroupsResponse[] adcServerGroupsResponse = response.as(AdcServerGroupsResponse[].class);
    .
    .
}

当我使用

评估响应对象时
response.getBody().asString()

我正在获取上述确切的json字符串。

但是在执行方法中的第二条命令之后

adcServerGroupsResponse = response.as(AdcServerGroupsResponse[].class);

并评估

adcServerGroupsResponseList.get(0)

我明白了

availabilityGroup = null
cloud = "1"
fqdns = null
groupType = null
healthCheck = null
name = "t1app1"
scalingGroup = null
selectionMetric = null
serverAddresses = null
serverSubnet = null

顺便说一句,

response.getBody().as(AdcServerGroupsResponse[].class)

List<AdcServerGroupsResponse> adcServerGroupsResponses = response.jsonPath().getList("", AdcServerGroupsResponse.class);

给我相同的结果...

那是为什么?

请告知。 谢谢!

1 个答案:

答案 0 :(得分:1)

这是json属性名称和类成员名称之间的命名不匹配。它们必须相同。因此,例如,成员名称也必须是availabilityGroup,而不是类中的availability_group和json中的availability_group