2017-03-02 77 views
1

這是我想要發佈的Jsonbody,並且我爲子節點(例如Phone,Fax)和Info(父節點)創建了POJO。我無法獲取子節點元素,信息元素工作得很好。RestAssured,無法在io.RestAssured中傳遞嵌套json的POJO對象

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類。 我爲每個設置每個類中的值創建了一個新對象。 ofcourse我的信息類有以下幾點:

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驗證爲崗位:我得到400

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

誰能告訴我,什麼問題呢?我在郵遞員嘗試相同,它的作品。我得到一個錯誤「無法識別的屬性‘電話’

+0

苦苦追隨你能分享一些細節,以便它很容易理解你正在面對的是什麼 – Coder

回答

0

在你的情況的電話和傳真不應HashMap的只是平面的物體下面的例子將提供所需的輸出。

@Data //using lombok annotation to reduce code amount 
@AllArgsConstructor 
public class Phone { 
    private String countryCode; 
    private String areaCode; 
    private String dialNumber; 
} 

@Data 
public class Fax { 
@AllArgsConstructor 
    private String countryCode; 
    private String nationalPrefix; 
    private String areaCode; 
    private String dialNumber; 
} 

@Data 
@AllArgsConstructor 
public class Info { 
    private String name; 
    private String addressLine1; 
    private String city; 
    private String state; 
    private String zipCode; 
    private Phone phone; 
    private Fax fax; 
} 

public void test() { 
... 
given() 
     .when() 
      .contentType(ContentType.JSON) 
      .body(new Info("Auto Test", "4399 Apple ln", "BlahWonders", GA", "30555", 
        new Phone("1", "678", "3196864"), 
        new Fax("1", "1", "333", "3333333"))) 
... 
} 

但在以下情況下:

@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" 
    } 
    } 
} 
+0

感謝RocketRaccoon,我把它傳真到另一個pojo,即使hashmap也可以工作,但是有一個數據驗證錯誤實際上沒有通過測試。 –