我要发送 HashMap<String, String>
从 JS 应用程序到我的 Google 应用程序。我创建了一个 HashMapContainer
类如:Cloud Endpoints Collection Parameter .
Endpoint 方法定义如下:
public Entity myMethod(
@Named('param1') String param1,
@Nullable @Named('param2') HashMapContainer param2) {
//...
}
当我运行 API 生成时,发生了这个错误:
com.google.api.server.spi.config.validation.ApiConfigInvalidException:
Resource type 'class com.mason.server.entity.HashMapContainer'
in method 'endpoint.myMethod' should not be named.
因此,我删除了 @Named
注解。生成了 API,但显然,我没有收到 JS 应用程序发送的参数。我的 JavaScript 是这样的:
function doTransaction() {
var req = gapi.client.myApi.endpoint.myMethod({
'param1': 'FOO',
'param2': {
'value1':'foofoo',
'value2':'barbar',
'value3':'foobar'
}
});
req.execute(function(data) {
console.log(data);
});
}
如何获得 param2
如果不允许我使用 @Named
注释 ?
也许我的 JavaScript 有误?
最佳答案
Google Cloud Enpoints documentation说:
@Named: This annotation indicates the name of the parameter in the request that gets injected here. A parameter that is not annotated with @Named is injected with the whole request object.
基本上,据我了解,当您添加 @Named
注释时,参数将包含在请求 URL 的末尾:
http://end_point_url?parameter1=xxx¶meter2=yyy
显然支持@Named
注解的参数类型只有少数几种(我认为是int、long、String、Boolean及其对应的数组),因为你不能将整个hashmap附加到请求地址!
另一方面,如果您不使用 @Named
,参数将包含(注入(inject))在 POST 数据中。
为了使用适用于 JavaScript 的 Google API 客户端库在 HTTP 正文中发送参数,您只需将该参数包含到 JSON-RPC 请求中名为 resource
的对象中,如下所示:
var req = gapi.client.myApi.endpoint.myMethod({
'param1': 'FOO',
'resource': {
'param2': {
'value1':'foofoo',
'value2':'barbar',
'value3':'foobar'
}
}
});
API 客户端会自动发送 URL 中的 param1
和 POST 数据中的 param2
...
这在 this section 中有详细解释。 JavaScript 文档的 Google API 客户端库。
关于java - Cloud Endpoint 参数不应命名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17454531/