【问题标题】:Passing list of boxed primitives to Google Cloud Endpoint将盒装原语列表传递给 Google Cloud Endpoint
【发布时间】:2014-08-18 08:27:56
【问题描述】:

我在 Google Cloud Endpoints 中将列表作为方法参数而苦苦挣扎。

文件说

支持的参数类型如下:

  • java.util.Collection 一个参数类型

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

@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");
        }
    }
}

当我使用 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 作为方法参数,如果这是我做错了什么?

【问题讨论】:

    标签: java android google-app-engine google-cloud-endpoints


    【解决方案1】:

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

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

    【讨论】:

    • 嗯...这行得通,但是解决此问题的方法很奇怪。它真的看起来像“这不是一个错误,它是一个功能”的东西。特别是我的方法中的参数不能为空。
    • 这有点奇怪,因为注释被称为@Nullable,但更合适的名称应该是@QueryParameter,因为使用此注释会改变端点url中参数的处理方式查询参数的路径参数 - 因此 url 格式从 endpointurl/param 更改为 endpointurl?parameter=param
    • 请记住,您还需要对参数使用 set() 方法,而不是普通的方法参数。所以代替 api.testMethod(longList).execute();你会做 api.testMethod().setLongList(longList).execute()
    【解决方案2】:

    更直接的方法是在 API_METHOD 注解中添加路径属性,并且路径中不包含 List 参数。如here所述:“如果指定了路径,则参数可以不包含在路径中作为查询参数”

    在您的情况下,它应该如下所示:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-01-26
      • 2019-02-13
      • 2012-02-29
      • 1970-01-01
      • 1970-01-01
      • 2014-01-06
      相关资源
      最近更新 更多