java - Cloud Endpoint 参数不应命名

标签 java javascript google-app-engine google-api google-cloud-endpoints

我要发送 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&parameter2=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/

相关文章:

java - Java 中覆盖成员变量(变量隐藏)

java - 使用可编辑组合框时,DefaultListCellRenderer 无法正确呈现空字符串

javascript - 如何让 window.crypto.subtle 输出与 'crypto' js 库相同的签名?

javascript - Three.js 转换一个选取数组

google-app-engine - 如何在 Google App Engine 上部署 Vue.js 应用程序?

python - Google App Engine 中的争用问题

java - webView帮助-错误页面INTERNET WebView

java - 如何从单个 Web 服务类调用不同的方法

javascript - Webpack-dev-server 找不到模块

java - 在 App Engine 上执行 future 某个时间任务的最有效方法是什么?