How to Receive Webhook from Stripe in Java

后端 未结 4 1867
眼角桃花
眼角桃花 2021-02-06 10:13

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(    consu         


        
4条回答
  •  忘掉有多难
    2021-02-06 10:34

    In order to abstract all of the deserialization logic out of the controller I did the following:

    Created a custom deserializer

    public class StripeEventDeserializer extends JsonDeserializer {
    
        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;
        }
    }
    

    I then needed to add that deserializer to my ObjectMapper config:

    SimpleModule simpleModule = new SimpleModule();
    simpleModule.addDeserializer(Event.class, new StripeEventDeserializer(mapper));
    mapper.registerModule(simpleModule);
    

    I could then use @RequestBody correctly on the Spring rest controller:

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

提交回复
热议问题