I am trying to receive a webhook via a post request from Stripe Payments. The java method to process it looks like this:

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

}

But the Stripe Event always comes back with all null values:

JSON: {

"id": null,

"type": null,

"user_id": null,

"livemode": null,

"created": null,

"data": null,

"pending_webhooks": null

}

If the method receives a String instead,and using @RequestBody:

@ResponseBody

@RequestMapping( consumes="application/json",

produces="application/json",

method=RequestMethod.POST,

value="stripeWebhookEndpoint")

public String stripeWebhookEndpoint(@RequestBody String json){

logger.info(json);

return null;

}

Here, it prints the json without null values. Here's part of the request being printed:

{

"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",

...

}

But using @RequestBody with a Stripe Event parameter gives a 400: bad syntax.

So why can't I take in the correct type, a Stripe Event, as the parameter?

解决方案

Here's what I did:

The Java method still takes in the Event as a json String. Then I used Stripe's custom gson adapter and got the Event with:

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

Where stripeJsonEvent is the string of json taken in by the webhook endpoint.

更多推荐

stripe java实现,如何在Java中从Stripe接收Webhook