1

I know How to get the @RequestBody in an @ExceptionHandler (Spring REST) exists but I am looking for a slightly different solution. When my service gets a 403 it does not make it to my @Controller class. Thus, how can I still grab something out of the incoming request body, since this scenario would never actually hit my code? It only ever hits the generated CGLIB code.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Brian
  • 556
  • 6
  • 26
  • for 403 in particular, i would recommend this: https://www.baeldung.com/exception-handling-for-rest-with-spring#denied – xerx593 Apr 07 '20 at 20:11
  • Which part of that article do recommend for this issue? – Brian Apr 07 '20 at 20:13
  • the linked: #denied - Chapter 6 :) ...6.2! – xerx593 Apr 07 '20 at 20:15
  • I had read that part, and that is basically what I am doing. The issue is, how do I get the @RrequestBody from that? Any object input to this service has a parent class in which a unique ID is required input by the client. I need to grab that ID from the input request, so I can log it before I return the error. So when my consumer complains of an issue for that specific ID, I can tell them they got a 403. – Brian Apr 07 '20 at 20:18
  • Once you have a `HttpServletRequest` (injected into your error handler), you can try to ["Get the POST request body from HttpServletRequest"](https://stackoverflow.com/q/8100634/592355). – xerx593 Apr 07 '20 at 20:38
  • I tired that too, but keep getting java.lang.IllegalStateException: getInputStream() has already been called for this request – Brian Apr 07 '20 at 20:43
  • this means: someone/some filter/interceptor has already accessed `request.getReader()` ... :-( – xerx593 Apr 07 '20 at 20:45
  • Yup I figured as much :( Problem is, its gotta be some spring magic doing it, because I haven even gotten to my code yet, nor do I have any other interceptors :( – Brian Apr 07 '20 at 20:46

1 Answers1

0

Ultimately I was able to solve the issue by creating a class that extends RequestBodyAdviceAdapter.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Brian
  • 556
  • 6
  • 26