I am struggling with Lists as method parameters at Google Cloud Endpoints.
我正在努力将Lists作为Google Cloud Endpoints的方法参数。
The documentations says that
文件说明了这一点
The supported parameter types are the following:
支持的参数类型如下:
- java.util.Collection of a parameter type
- java.util.Collection参数类型
I tried to do it this way, but it just do not work. Basic endpoint method:
我试着这样做,但它只是不起作用。基本端点方法:
@ApiMethod(name = "testMethod", httpMethod = HttpMethod.POST)
public void testMethod(@Named("longList") List<Long> longList) {
for (Long aLong : longList) {
if (aLong < 5) {
throw new IllegalArgumentException("You can't do it");
}
}
}
When I execute this method using API Exploler the generated URL is:
当我使用API Exploler执行此方法时,生成的URL为:
POST http://localhost:8080/_ah/api/billEndpoint/v1/testMethod?longList=5&longList=6
And the method is executed correctly.
并且该方法正确执行。
But when Android library is used the url is changed to:
但是当使用Android库时,url会更改为:
http://APP_ENGINE_BACKEND:8080/_ah/api/billEndpoint/v1/testMethod/5,6
and the endpoint return 404 code.
并且端点返回404代码。
It is possible to have List as method parameter and if it is what I am doing wrong?
可以将List作为方法参数,如果它是我做错了吗?
2 个解决方案
#1
2
Please add @Nullable annotation to your method, that will turn your collection-type parameter from a path into a query parameter.
请将@Nullable注释添加到您的方法中,这会将您的集合类型参数从路径转换为查询参数。
https://developers.google.com/appengine/docs/java/endpoints/annotations#nullable
https://developers.google.com/appengine/docs/java/endpoints/annotations#nullable
#2
1
A more direct way is to add a path property to the API_METHOD annotation and not include the List parameter in the path. As stated here: "If path is specified, parameters can be made query parameters by not including them in the path"
更直接的方法是向API_METHOD注释添加路径属性,而不在路径中包含List参数。如下所述:“如果指定了路径,则可以通过不在路径中包含参数来创建参数查询参数”
In your case it should look like:
在你的情况下,它应该看起来像:
@ApiMethod(name = "testMethod", path="testMethod" httpMethod = HttpMethod.POST)
@ApiMethod(name =“testMethod”,path =“testMethod”httpMethod = HttpMethod.POST)
#1
2
Please add @Nullable annotation to your method, that will turn your collection-type parameter from a path into a query parameter.
请将@Nullable注释添加到您的方法中,这会将您的集合类型参数从路径转换为查询参数。
https://developers.google.com/appengine/docs/java/endpoints/annotations#nullable
https://developers.google.com/appengine/docs/java/endpoints/annotations#nullable
#2
1
A more direct way is to add a path property to the API_METHOD annotation and not include the List parameter in the path. As stated here: "If path is specified, parameters can be made query parameters by not including them in the path"
更直接的方法是向API_METHOD注释添加路径属性,而不在路径中包含List参数。如下所述:“如果指定了路径,则可以通过不在路径中包含参数来创建参数查询参数”
In your case it should look like:
在你的情况下,它应该看起来像:
@ApiMethod(name = "testMethod", path="testMethod" httpMethod = HttpMethod.POST)
@ApiMethod(name =“testMethod”,path =“testMethod”httpMethod = HttpMethod.POST)