在Java中创建一个能够接受来自Webhook的POST请求的API端点

时间:2018-10-16 08:55:25

标签: java json webhooks

我有一个Webhook设置,在上传后,文件服务器会使用HTTP POST将通知发送到Webhook测试器URL。在webhook配置文件中,将Webhook测试器URL分配为端点。例如,当一个名为image1.jpg的文件上传到微型服务器中名为images的存储桶时,我在Webhook Tester的端点中得到了这样的JSON数据:

{
"EventName": "s3:ObjectCreated:Put",
"Key": "images/image1.jpg",
"Records": [
{
  "eventVersion": "2.0",
  "eventSource": "minio:s3",
  "awsRegion": "",
  "eventTime": "2018-10-16T05:53:23Z",
  "eventName": "s3:ObjectCreated:Put",
  "userIdentity": {
    "principalId": "EZXTCHKE2YHUNRPI8JCL"
  },
  "requestParameters": {
    "sourceIPAddress": "127.0.0.1"
  },
  "responseElements": {
    "x-amz-request-id": "155E00FBBE5DC52B",
    "x-minio-origin-endpoint": "http://127.0.0.1:9000"
  },
  "s3": {
    "s3SchemaVersion": "1.0",
    "configurationId": "Config",
    "bucket": {
      "name": "images",
      "ownerIdentity": {
        "principalId": "EZXTCHKE2YHUNRPI8JCL"
      },
      "arn": "arn:aws:s3:::images"
    },
    "object": {
      "key": "image1.jpg",
      "size": 374960,
      "eTag": "eeee5654de91dd9295590449405b4a2c",
      "contentType": "image/jpeg",
      "userMetadata": {
        "content-type": "image/jpeg"
      },
      "versionId": "1",
      "sequencer": "155E00FBBE5DC52B"
    }
  },
  "source": {
    "host": "",
    "port": "",
    "userAgent": "Minio (linux; amd64) minio-go/v6.0.6 mc/2018-09-26T00:42:43Z"
  }
}
]
}

如何在jax-rs中实现类似的功能?我该如何创建一个能够处理POST请求的API端点,类似于可生成JSON数据的Webhook测试器?

1 个答案:

答案 0 :(得分:0)

我设法通过创建一个POST资源处理程序来做到这一点:

@POST
@Path("webhook")
@Produces(MediaType.APPLICATION_JSON)
public Response webhookListener(NotificationConfiguration nc) throws JsonProcessingException {
    ObjectMapper mapper = new ObjectMapper();
    String jsonString = mapper.writeValueAsString(nc);
    System.out.println(jsonString);
    return Response.ok().entity(jsonString).build();
}

NotificationConfiguration是一种数据类型,小型服务器使用该数据类型发送webhook通知,然后使用杰克逊将NotificationConfiguration转换为JSON,从而获得所需的JSON数据。