【问题标题】:JSON conversion issue from JS to Google Cloud Endpoints server从 JS 到 Google Cloud Endpoints 服务器的 JSON 转换问题
【发布时间】:2013-07-27 04:20:46
【问题描述】:

执行调用时出现服务器异常:

var x = '{"name": "Test","address": { "street": "White House"}}';
gapi.client.realestate.create(x).execute(function(resp) {
    console.log(resp);
});

java.lang.ClassCastException: com.google.appengine.repackaged.org.codehaus.jackson.node.TextNode cannot be cast to com.google.appengine.repackaged.org.codehaus.jackson.node.ObjectNode
    at com.google.api.server.spi.tools.devserver.RpcApiServlet.doSingleBackendCall(RpcApiServlet.java:131)
    at com.google.api.server.spi.tools.devserver.RpcApiServlet.service(RpcApiServlet.java:112)

如果我在API Explorer 中使用相同的 JSON 表达式,它就可以工作。 @ApiMethod 定义为:

@ApiMethod(name = "create", path = "properties", httpMethod = HttpMethod.POST)
public void create(RealEstateProperty property, User user) throws Exception {

【问题讨论】:

    标签: google-cloud-endpoints


    【解决方案1】:

    JS 客户端库方法需要 JSON 对象而不是 JSON 字符串,它为调用本身进行编码。此外,由于这是一个 POST 请求,您必须将 POST 主体定义为该方法的 resource 参数(不幸的是,它并没有很好地记录)

    这应该可行:

    var x = {"name": "Test","address": { "street": "White House"}};
    gapi.client.realestate.create({"resource": x}).execute( ...
    

    【讨论】:

    • 我没有异常了,但是服务器端的property 对象没有被填充。实例已创建,但字段为 null
    • @Sydney,看看我的问题here
    猜你喜欢
    • 1970-01-01
    • 2022-08-09
    • 2017-06-11
    • 2018-07-06
    • 1970-01-01
    • 1970-01-01
    • 2018-09-23
    • 2018-08-14
    • 2020-08-18
    相关资源
    最近更新 更多