2014-09-03 33 views
2

我在Google Cloud Endpoint模塊中有一個Api方法,它返回一個Collection。該方法看起來就像這樣:當ApiMethod返回一個集合時,有沒有辦法擺脫包裝項屬性?

@ApiMethod(name="listChildren") 
public Collection<Child> listChildren() { 
    Collection<Child> children = getChildren(); //call some method to build the collection 
    return children; 
} 

當我看到返回的JSON響應,我的反應看起來就像這樣:

{ 
    "items": [ 
     { ... }, // first child 
     { ... }, // second child 
     ... // and so on 
    ] 
} 

我試圖擺脫的「物品」的屬性,將項目包裝在我的集合中,以便JSON響應直接是數組。我期待得到更像下面的迴應:

[ 
    { ... }, // first child 
    { ... }, // second child 
    ... // and so on 
] 

有沒有辦法實現這一點?我幾次瀏覽了Google Cloud Endpoints文檔,但沒有結果。我想有一個原因,爲什麼響應總是包裹在「項目」屬性中,但我無法弄清楚原因。

回答

0

如果你不是返回一個Collections而是返回一個Array,會發生什麼?

@ApiMethod(name="listChildren") 
public Child[] listChildren() { 
    Collection<Child> children = getChildren(); //call some method to build the collection 
    return children.toArray(); 
} 

而且您可以隨時創建自己的ApiTransformer,它可以創建沒有「items」屬性的返回。 更多關於https://developers.google.com/appengine/docs/java/endpoints/annotations#apitransformer

+0

我試了兩種解決方案無濟於事。返回一個'Child []'給出相同的結果。爲了使用ApiTransformer擴展一個'Collection'將返回一個JSON格式如下:'{「items」:「從我的ApiTransformer轉換的集合」}' – bernyfox 2014-09-04 15:12:36

相關問題