Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Java中从Stripe接收Webhook_Java_Json_Rest_Stripe Payments_Webhooks - Fatal编程技术网

如何在Java中从Stripe接收Webhook

如何在Java中从Stripe接收Webhook,java,json,rest,stripe-payments,webhooks,Java,Json,Rest,Stripe Payments,Webhooks,我正试图通过一个来自Stripe Payments的post请求接收webhook。处理它的java方法如下所示: @ResponseBody @RequestMapping( consumes="application/json", produces="application/json", method=RequestMethod.POST, value="stripe

我正试图通过一个来自Stripe Payments的post请求接收webhook。处理它的java方法如下所示:

@ResponseBody
@RequestMapping(    consumes="application/json",
                    produces="application/json",
                    method=RequestMethod.POST,
                    value="stripeWebhookEndpoint")
public String stripeWebhookEndpoint(Event event){

    logger.info("\n\n" + event.toString());

    logger.info("\n\n" + event.getId());

    return null;
}
但条带事件总是返回所有空值:

<com.stripe.model.Event@315899720 id=null> JSON: {
  "id": null,
  "type": null,
  "user_id": null,
  "livemode": null,
  "created": null,
  "data": null,
  "pending_webhooks": null
}
在这里,它打印的json没有空值。以下是正在打印的请求的一部分:

{
  "created": 1326853478,
  "livemode": false,
  "id": "evt_00000000000000",
  "type": "charge.succeeded",
  "object": "event",
  "request": null,
  "data": {
    "object": {
      "id": "ch_00000000000000",
      "object": "charge",
      "created": 1389985862,
      "livemode": false,
      "paid": true,
      "amount": 2995,
      "currency": "usd",
...
}
但是使用带条带事件参数的@RequestBody会产生400:bad语法


那么,为什么我不能将正确的类型(条带事件)作为参数呢?

我一直在寻找相同的答案,所以在查看了他们自己的代码后,他们实际上是如何做到的:

String rawJson = IOUtils.toString(request.getInputStream());
Event event = APIResource.GSON.fromJson(rawJson, Event.class);
APIResource来自他们的库(我使用的是1.6.5)

以下是我所做的:

Java方法仍然将事件作为json字符串接收。然后,我使用Stripe的自定义gson适配器并获得了以下事件:

Event event = Event.gson.fromJson(stripeJsonEvent, Event.class);

其中stripeJsonEvent是webhook端点接收的json字符串。

为了从控制器中提取所有反序列化逻辑,我执行了以下操作:

public String stripeWebhookEndpoint(@RequestBody String json, HttpServletRequest request) {         
        String header = request.getHeader("Stripe-Signature");      
        String endpointSecret = "your stripe webhook secret";
        try {
            event = Webhook.constructEvent(json, header, endpointSecret);
            System.err.println(event);
        } catch (SignatureVerificationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
         //
         enter code here
      return "";

}
创建了自定义反序列化程序

public class StripeEventDeserializer extends JsonDeserializer<Event> {

    private ObjectMapper mapper;
    
    public StripeEventDeserializer(ObjectMapper mapper) {
        this.mapper = mapper;
    }

    @Override
    public Event deserialize(JsonParser jp, DeserializationContext context) throws IOException {
        ObjectNode root = mapper.readTree(jp);

        Event event = ApiResource.GSON.fromJson(root.toString(), Event.class);
        return event;
    }
}
然后,我可以在Spring rest控制器上正确使用
@RequestBody

@PostMapping("/webhook")
public void webhook(@RequestBody Event stripeEvent)

嗨,胡安+1用于使用资源。关于如何做,我也有一个答案。请求来自哪个包?如何获取事件对象的子对象的属性。假设我想从事件对象获取订阅id。我无法通过执行event.get….()直接访问它,因为它不可用。谢谢您的提问,但是它可以从stripe中点击
localhost
url?从库的1.25版开始,它是
event.GSON.fromJson(stripeJsonEvent,event.class)
SimpleModule simpleModule = new SimpleModule();
simpleModule.addDeserializer(Event.class, new StripeEventDeserializer(mapper));
mapper.registerModule(simpleModule);
@PostMapping("/webhook")
public void webhook(@RequestBody Event stripeEvent)