2016-07-27 45 views
-2

我正在用汽車的集合資源創建休息服務, 在代碼中,我設置集合的ID供以後使用, 但是,當我返回CarResources的響應該ID缺失。 另外,IDE似乎不是 '看' 這個屬性擴展ArrayList導致澤西島,Java中缺少的屬性

代碼不:

public class CarResources extends ArrayList<CarResource> { 

    private String id; 

    public void setId(String id) { 
    this.id = id; 
    } 

    public String getId() { 
    return id; 
    } 


    public CarResources (String id) { 
    this.id = id; 
    } 

響應:

[ 
//Missing in the response is an id property of the entire List 
    { 
    "CarResource": { 
     "id": "TOYOTA_CAMERI", 
     "name": "Toyota", 
     "carType": "GAS" 
     } 
    }, 
    { 
    "CarResource": { 
     "id": "HONDA_TYPE_R", 
     "name": "HONDA", 
     "carType": "GAS" 
     } 
    } 


] 
+2

不擴展ArrayList。創建一個具有List作爲字段的自定義類 –

回答

3

最簡單的辦法是延長ArrayList,只是有列表作爲您的自定義對象的成員,它將序列化,而無需任何額外的努力。在這種情況下繼承是不正確的解決方案,組成是。

如果你堅持擴大ArrayList你需要@JsonProperty註釋你的新實例成員和/或編寫自定義JsonSerializer<CarResources>和註釋類@JsonSerialize使用新的自定義序列化,因爲默認ArrayList串行被以其它方式使用。

+0

謝謝我已經添加了受保護的ArrayList 數據;到我們的BaseCollection資源並解決它。 –

+0

@Nir不要添加謝謝的評論考慮[接受](http://stackoverflow.com/help/accepted-answer)回答檢查旁邊的綠色檢查回答 –