0

Giving the following code:

    @PostMapping(value = "/wc-order")
    public void getWcOrder(@RequestBody Order order, @RequestHeader Map<String, String> headers, @RequestBody String plainJsonBody){
        String webhookSignature = headers.get("x-wc-webhook-signature");
        String hashedBody = doHMAC(plainJsonBody);
        if (hashedBody.equals(webhookSignature)) {
            // all good
        }
    }

The JSON body will successfully be mapped to the Order object.

Furthermore I would like to have the JSON body as well as a string to finally hash it and compare with the signature. Unfortunately I don't know how to extract plain body without loosing the capability to map it to order Object.

Is there a simple way to achieve it?

I've tried as in How to access plain json body in Spring rest controller?

But here I loose the capability of the deserialization to the Order object.

Fabo137
  • 46
  • 4
  • 1
    You do not lose capability to deserialize the post body contents into Order object if you read the contents as a string only. You can use `new JsonMapper().readValue(rawJsonString, Order.class)` or similar construct to do same as `@PostMapping` does for the object by default. The return value is Order object filled with values from the JSON string - or an exception in case deserialization fails. – harism Jan 22 '23 at 01:09

1 Answers1

0

you cant use two RequestBody inside the same Request, you have three options to throw with your scenario

1- the first one merge plainJsonBody inside order as a variable and set @NotBalnk on it.

2- you can change plainJsonBody to @RequestParam and set the param as a reuired=true

3- you can replace all @RequestBody to @RequestPart`

Abdalrhman Alkraien
  • 645
  • 1
  • 7
  • 22
  • 1
    There is also option 4 - opt to read request body as a string only and deserialize it in code throwing a `HttpStatus.BAD_REQUEST` if that fails. – harism Jan 22 '23 at 00:58