Passing list of boxed primitives to Google Cloud E

2019-02-20 15:52发布

I am struggling with Lists as method parameters at Google Cloud Endpoints.

The documentations says that

The supported parameter types are the following:

  • java.util.Collection of a parameter type

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:

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:

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

and the endpoint return 404 code.

It is possible to have List as method parameter and if it is what I am doing wrong?

2条回答
Ridiculous、
2楼-- · 2019-02-20 16:06

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"

In your case it should look like:

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

查看更多
Luminary・发光体
3楼-- · 2019-02-20 16:16

Please add @Nullable annotation to your method, that will turn your collection-type parameter from a path into a query parameter.

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

查看更多
登录 后发表回答