云端点参数不应该被命名 [英] Cloud Endpoint parameter should not be named

查看:74
本文介绍了云端点参数不应该被命名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从JS应用程序发送一个 HashMap< String,String> 到我的Google App。我创建了 HashMapContainer 类,如下所示: Cloud Endpoints收集参数


Endpoint方法定义如下:

  public entity myMethod(
@Named('param1')String param1,
@Nullable @Named('param2')HashMapContainer param2){
// .. 。
}

当我运行API生成这个错误时:

  com.google.api.server.spi.config.validation.ApiConfigInvalidException:
资源类型'class com.mason.server.entity方法'endpoint.myMethod'中的.HashMapContainer'
不应该被命名。

因此,我删除了 @Named 注释。 API已生成,但很明显,我没有收到JS应用程序发送的参数。我的JavaScript是这样的:

 函数doTransaction(){
var req = gapi.client.myApi.endpoint。 myMethod({$ b $'param1':'FOO',
'param2':{$ b $'value1':'foofoo',
'value2':'barbar',
'value3':'foobar'
}
});
req.execute(function(data){
console.log(data);
});

$ / code>

如何获取 param2 如果我不允许使用 @Named 注释?

也许我的JavaScript错了?


解决方案

Google Cloud Enpoints 文档说:


@Named:此注释表示在这里注入的
请求中的参数。使用@Named注释
的参数将注入整个请求对象。


基本上,据我了解,当您添加 @Named 注释时,参数将包含在请求URL的末尾:

  http:// end_point_url?parameter1 = xxx& parameter2 = yyy 

很明显,支持 @Named 注释的参数类型只有少数(int,long,String,Boolean和它们的对应数组,我认为),因为你另一方面,如果你不使用 @Named

为了在HTTP正文中发送一个参数,使用$ ,参数将包含在POST数据中。 用于JavaScript的Google API客户端库,您只需将该参数包含在JSON-RPC请求中的名为 resource 的对象中t,像这样:

  var req = gapi.client.myApi.endpoint.myMethod({
'param1' :'FOO',
'资源':{
'param2':{$ b $'value1':'foofoo',
'value2':'barbar',
'value3':'foobar'
}
}
});

API客户端会自动发送 param1 in POST数据中的URL和 param2 ...



这在 此部分 的Google API客户端库JavaScript文档。


I want send a HashMap<String, String> from JS application to my Google App. I created a HashMapContainer class such as in : Cloud Endpoints Collection Parameter.

The Endpoint method is defined like this :

public Entity myMethod(
        @Named('param1') String param1, 
        @Nullable @Named('param2') HashMapContainer param2) {
    //...
}

When I run the API generation this error happens :

com.google.api.server.spi.config.validation.ApiConfigInvalidException: 
    Resource type 'class com.mason.server.entity.HashMapContainer' 
    in method 'endpoint.myMethod' should not be named.

Therefore, I remove the @Named annotation. The API is generated but obviously, I do not receive the parameter send by the JS application. My JavaScript is like this :

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

How I can get the param2 if I'm not allowed to use the @Named annotation ?
Maybe my JavaScript is wrong?

解决方案

Google Cloud Enpoints documentation says:

@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.

Basically, as far as I understand, when you add @Named annotation, the parameters will be included at the end of the request URL:

http://end_point_url?parameter1=xxx&parameter2=yyy

Obviously the parameter types that support @Named annotation are only a few (int, long, String, Boolean and their correspondent arrays, I think), because you can't append a whole hashmap to the request URL!

On the other hand, if you don't use @Named, the parameter will be included (injected) within the POST data.

In order to send a parameter within the HTTP body using the Google APIs Client Library for JavaScript, you just have to include that parameter into an object called resource inside the JSON-RPC request, like this:

var req = gapi.client.myApi.endpoint.myMethod({
    'param1': 'FOO',
    'resource': {
        'param2': {
            'value1':'foofoo',
            'value2':'barbar',
            'value3':'foobar'
        }
    }
});

The API client will automatically send param1 in the URL and param2 in the POST data...

This is explained in detail in this section of the Google APIs Client Library for JavaScript documentation.

这篇关于云端点参数不应该被命名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆