I know I am not the first one to ask this question, and I could post several links on StackOverflow where this question was asked, but I haven't seen where my problem is specifically addressed.
So, I have a Spring 5 Framework RESTful Controller setup, this is not Spring Boot.
@PostMapping(value = "/webhook-endpoint")
public HttpStatus stripeWebhookEndpoint(
@RequestBody String stripeJsonEvent,
@RequestHeader("Stripe-Signature") String sigHeader)
{
System.out.println("stripeWebhookEndpoint: START: json = " + stripeJsonEvent);
return HttpStatus.OK;
}
Judging by the other examples I have seen, this absolutely should work. But in my case, I am getting an HTTP 400 Error as follows:
[http-nio-8080-exec-7] WARN org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver -
Resolved [org.springframework.http.converter.HttpMessageNotReadableException:
JSON parse error: Cannot deserialize instance of `java.lang.String` out of START_OBJECT token;
nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException:
Cannot deserialize instance of `java.lang.String` out of START_OBJECT token
at [Source: (PushbackInputStream); line: 1, column: 1]]
I have even tried adding: consumes = "text/plain; application/json" and I get the same error. If I can get just get the JSON, then I'll be fine after that. There will be plenty of examples to parse this Stripe event.
What did work was when I had this configuration:
@RequestBody Object stripeJsonEvent
And then I could see the Json as part of the object, but then I couldn't cast from the Object to String.
I think what is important to add here is that I am using the Stripe CLI to create this call to my controller. With Stripe CLI the first thing I did was create the listener: stripe listen --forward-to localhost:8080/myapp/api/stripe/webhook-endpoint
In another Terminal window I did: stripe trigger payment_method.attached
So, this is the error I have. I'm going to try from another REST client like PostMan and see if I get the same results. I'll keep playing with it and see if I can come up with another fix.
Thanks!