【问题标题】:How to retrieve JSON from HTTP Post request inside Google Endpoint method?如何从 Google Endpoint 方法中的 HTTP Post 请求中检索 JSON?
【发布时间】:2016-05-21 13:54:48
【问题描述】:

我已按照设置从​​ Gmail API 获取推送通知到我的应用程序,如下所示:https://developers.google.com/gmail/api/guides/push

我已配置如下所示的 Google Endpoint 方法来接收来自 Gmail 的 HTTP POST 回调(Webhook 推送)。这是有效的,并会在新电子邮件中被调用。

@ApiMethod(
            name = "myhandler",
            path = "_ah/push-handlers/myhandler",
            httpMethod = ApiMethod.HttpMethod.POST
    )
    public void myhandler(NotificationMessage notificationMessage) throws IOException {

        //process message

    }

现在,来自 Gmail 的回调在请求的 HTTP POST 正文中发送以下 JSON 结构:

POST https://yourserver.example.com/yourUrl
Content-type: application/json

{
  message:
  {
    // This is the actual notification data, as base64url-encoded JSON.
    data: "eyJlbWFpbEFkZHJlc3MiOiAidXNlckBleGFtcGxlLmNvbSIsICJoaXN0b3J5SWQiOiAiMTIzNDU2Nzg5MCJ9",

    // This is a Cloud Pub/Sub message id, unrelated to Gmail messages.
    message_id: "1234567890",
  }

  subscription: "projects/myproject/subscriptions/mysubscription"
}

我正在尝试使用我这样定义的 NotificationMessage 对象检索这些数据:

public class NotificationMessage {
    JSONObject message;

    public JSONObject getMessage() {
        return message;
    }

    public void setMessage(JSONObject message) {
        this.message = message;
    }
}

我应该如何更改此类或更改我的 Endpoint 方法以从上述 HTTP POST 请求中检索更深一层的 message.data JSON 值?

我检查了类似的问题,但没有找到任何显示如何检索嵌入在 POST 请求的 JSON 中的值,特别是在 Google Java Endpoint 方法中。

有什么建议吗?

【问题讨论】:

    标签: java android json post endpoint


    【解决方案1】:

    对此的更新:

    同时,在 http://www.jsonschema2pojo.org/ 的帮助下,我能够获得与问题中提到的 JSON 数据对应的 Java 类,并在 Endpoint API 中使用了这些类。现在可以正常使用了!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-11-26
      • 1970-01-01
      • 1970-01-01
      • 2020-01-09
      • 2011-08-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多