使用 @Named 和未命名参数从 Javascript 客户端库进行调用是没有意义的

2024-02-07

我有一个Cloud Endpoints方法看起来像这样:

//HTTP POST
@ApiMethod(name = "hylyts.insert")
public Hylyt insertHylyt(@Named("url") String url, Hylyt hylyt, User user)
        throws OAuthRequestException{
    log.info("Trying to save hylyt '"+hylyt+"' with id '"+hylyt.getId());
    if (user== null) throw new OAuthRequestException("Your token is no good here.");
    hylyt.setArticle(getArticleKey(url, user));
    ofy().save().entity(hylyt);
    return hylyt;
}

我称它为Javascript Client Library使用这个:

gapi.client.hylytit.hylyts.insert({PARAMS}).execute(callback);

现在,如果我构造{PARAMS}如建议的docs https://developers.google.com/api-client-library/javascript/dev/dev_jscript#ExampleGoogleCloudStorage(第二个例子),

{
  'url': url,
  'resource': {
    'hylyt': {
      'contentType': 'application/json',
      'data': hylyt
    }
  }
}

我在端点中得到一个空对象(更不用说这个库的全部目的是使这些调用变得简单,而这个结构显然违反了这一点)。

当我构建{PARAMS} as these https://stackoverflow.com/a/17458007/1720014 answers https://stackoverflow.com/questions/19804951/google-cloud-endpoints-making-calls-with-js-client-passing-params-and-json-bo/19809245#19809245建议,

{
  'url': url,
  'resource': hylyt
}

我再次在端点中得到一个空对象。正确的语法是这样的:

{
  'url': url,
  'id': hylyt.id
  'text': hylyt.text
}

这让我大吃一惊。难道我做的这一切都是错的吗?这是一个错误吗?难道这只是因为gapi也正在通过auth token在后台?

是的,我可以使用request语法,但是,再说一次,如果它和制作一样复杂,为什么还要使用该库XHR是纯javascript吗?如果谷歌在文档中解释,我不会介意复杂性why事情正在发生。但文档中解释说,只是说使用这些方法,并且身份验证、CORS 和 XHR 魔法将在幕后发生。


API 方法是否正确识别为POST method?

作为 POST 正文发送的资源参数在GET要求。 看起来你实际上是在发送一个GET请求在查询字符串中包含 Hylyt 属性。

为了确保您可以将方法注释更改为:

@ApiMethod(name = "hylyts.insert", httpMethod = HttpMethod.POST)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 @Named 和未命名参数从 Javascript 客户端库进行调用是没有意义的 的相关文章

随机推荐