Mat*_*att 6 java rest json webhooks stripe-payments
我正在尝试通过Stripe Payments的帖子请求接收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;
}
Run Code Online (Sandbox Code Playgroud)
但Stripe事件总是返回所有空值:
<com.stripe.model.Event@315899720 id=null> JSON: {
"id": null,
"type": null,
"user_id": null,
"livemode": null,
"created": null,
"data": null,
"pending_webhooks": null
}
Run Code Online (Sandbox Code Playgroud)
如果方法接收到String,并使用@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;
}
Run Code Online (Sandbox Code Playgroud)
在这里,它打印没有空值的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",
...
}
Run Code Online (Sandbox Code Playgroud)
但是使用带有Stripe Event参数的@RequestBody 会产生400:错误的语法.
那么为什么我不能接受正确的类型,条纹事件,作为参数?
小智 11
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 "";
}
Run Code Online (Sandbox Code Playgroud)
Mat*_*att 10
这是我做的:
Java方法仍然将事件作为json String接收.然后我使用了Stripe的自定义gson适配器并获得了以下事件:
Event event = Event.gson.fromJson(stripeJsonEvent, Event.class);
Run Code Online (Sandbox Code Playgroud)
其中stripeJsonEvent是webhook端点接收的json字符串.
我一直在寻找相同的答案,所以在看了他们自己的代码后,他们实际上是这样做的:
String rawJson = IOUtils.toString(request.getInputStream());
Event event = APIResource.GSON.fromJson(rawJson, Event.class);
Run Code Online (Sandbox Code Playgroud)
APIResource来自他们的图书馆(我使用的是1.6.5)
| 归档时间: |
|
| 查看次数: |
6175 次 |
| 最近记录: |