RestAssured,无法在io.RestAssured中传递嵌套json的POJO对象

时间:2017-03-02 21:59:40

标签: json pojo rest-assured

这是我要发布的Jsonbody,我为子节点创建了POJO,如Phone,传真以及Info(父节点)。我无法获取子节点元素,信息元素工作得很好。

info
        {
          "Name": "Auto Test",
          "addressLine1": "4399 Apple ln",
          "city": "BlahWonders",
          "state": "GA",
          "zipCode": "30555",
          "phone": {
            "countryCode": "1",
            "areaCode": "678",
            "dialNumber": "3196864"
          },
          "fax": {
            "countryCode": "1",
            "nationalPrefix": "1",
            "areaCode": "333",
            "dialNumber": "3333333"
          },

    }

我为各种类别的信息,电话,传真创建了POJO课程。 我为每个设置每个类中的值创建了一个新对象。当然,我的Info类有以下内容:

private String name;
private String addressLine1;
private String city;
private String state;
private String zipCode;
private HashMap<String,Phone> phone;
private HashMap<String,Fax> fax;
Info info = new Info();
        Phone phone = new Phone();
        phone.setAreaCode("333");
        phone.setCountryCode("");
        phone.setDialNumber("666666");
        Fax fax = new Fax();
        fax.setAreaCode("555");
        fax.setCountryCode("usa");
        fax.setDialNumber("9999999");
        HashMap<String,Fax> faxMap= new HashMap<String,Fax>();
        faxMap.put("fax",fax);
        HashMap<String,Phone> phoneMap= new HashMap<String,Phone>();
        phoneMap.put("phone",phone);
        info.setFax(faxMap);
        info.setPhone(phoneMap);
.
.
.
.

RestAssured post for post:我得到400

given().contentType("application/json").body(info).when().post("/info/add");

有人能告诉我这是错的吗?我在邮差中尝试了同样的方法,但它确实有效。我收到错误“无法识别的属性'手机'

1 个答案:

答案 0 :(得分:0)

在您的情况下,电话和传真不应该是HashMap,而应该只是平面对象。以下示例将提供所需的输出:

@Data
@AllArgsConstructor
public class Info {
    private String name;
    private String addressLine1;
    private String city;
    private String state;
    private String zipCode;
    private Map<String, Phone> phones;
    private Map<String, Fax> faxes;
}

但是在以下情况下:

{
  "name": "Auto Test",
  "addressLine1": "4399 Apple ln",
  "city": "BlahWonders",
  "state": "GA",
  "zipCode": "30555",
  "phones": {
    "phone": {
      "countryCode": "1",
      "areaCode": "678",
      "dialNumber": "3196864"
    }
  },
  "faxes": {
    "fax": {
      "countryCode": "1",
      "nationalPrefix": "1",
      "areaCode": "333",
      "dialNumber": "3333333"
    }
  }
}

你会得到:

const element = document.getElementById('received-time');

const timeString = "2017-05-04T04:26:37.000Z";
const time = Date.parse(timeString);

const dayBeginning = new Date();
dayBeginning.setHours(0, 0, 0, 0);
const dayBeginningTime = dayBeginning.getTime();

if (time > dayBeginning) {
	element.innerHTML = 'today';
} else {
	element.innerHTML = 'not today'
}