将装箱原语列表传递给Google Cloud Endpoint

我正在努力将Lists作为Google Cloud Endpoints的方法参数。

文件说明了这一点

支持的参数类型如下:

  • java.util.Collection参数类型

我试着这样做,但它只是不起作用。 基本端点方法:

@ApiMethod(name = "testMethod", httpMethod = HttpMethod.POST) public void testMethod(@Named("longList") List longList) { for (Long aLong : longList) { if (aLong < 5) { throw new IllegalArgumentException("You can't do it"); } } } 

当我使用API​​ Exploler执行此方法时,生成的URL为:

 POST http://localhost:8080/_ah/api/billEndpoint/v1/testMethod?longList=5&longList=6 

并且该方法正确执行。

但是当使用Android库时,url会更改为:

 http://APP_ENGINE_BACKEND:8080/_ah/api/billEndpoint/v1/testMethod/5,6 

并且端点返回404代码。

可以将List作为方法参数,如果它是我做错了吗?

请将@Nullable注释添加到您的方法中,这会将您的集合类型参数从路径转换为查询参数。

https://developers.google.com/appengine/docs/java/endpoints/annotations#nullable

更直接的方法是向API_METHOD注释添加路径属性,而不在路径中包含List参数。 如下所述:“如果指定了路径,则可以通过不在路径中包含参数来创建参数查询参数”

在你的情况下,它应该看起来像:

@ApiMethod(name =“testMethod”,path =“testMethod”httpMethod = HttpMethod.POST)