首页 文章

Google Cloud Endpoints中的多个实体参数

提问于
浏览
3

如何将多个实体从客户端传递到Google Cloud endpoints ?

例如,在服务器的Endpoint api源文件中轻松完成传递单个实体:

public class SomeEndpoint {
...
   @ApiMethod(...)
   public MyEntity someMethod(MyEntity someEntity) {
   ...
   }
...
}

然后在客户端我可以轻松打电话

endpoint.someMethod(someEntity).execute()

但是,如果我想将两个实体传递给 endpoints 怎么办?,如下所示:

@ApiMethod(...)
 public MyEntity otherMethod(MyEntity someEntity, MyEntity someOtherEntity) {
    ...
 }

这不起作用,GPE只生成一个带有单个MyEntity参数的 endpoints 库 .

是否可以传递多个Entity参数?

谢谢 .

2 回答

  • 0

    您不能在请求正文中发送多个实体类型 . 您需要创建一个包含这两个实体的包装器实体,例如:

    class MyWrapperEntity {
      MyEntity someEntity;
      MyOtherEntity someOtherEntity;
      // ...
    }
    

    但是,这不是您的示例所示(实体是相同的类型) . 在集合实体中使用 List<MyEntity>Map<String, MyEntity> ,例如:

    class MyEntityCollection {
      List<MyEntity> items;
      // ...
    }
    
  • 9

    使用'named'注释...

    @ApiMethod(name = "sendStuff")
    public void sendStuff( @Named("clientId") String clientId, @Named("stuff") String stuff )
    

    对于Android,客户端代码看起来像这样

    SendStuff sl = service.sendStuff( clientId, stuff );
    

相关问题