2014-09-03 35 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

相关问题