With spring webflux and the @RestController
model, I have a @RestControllerAdvice
with some @ExceptionHandler
methods.
I would like to get the original request as parameter because I want to log it and use it in my response.
However I have tried all the possible types for an handler method in the classic MVC model but none of them was accepted (HttpServletRequest
, WebRequest
and ServerRequest
).
What type can I use to access the original request in a webflux annotated handler method ?
The request body can normally be obtained from here via getInputStream() or getReader() , but if my controller methods parse the request body like "@RequestBody Foo fooBody" as all of mine do, the HttpServletRequest's input stream or reader is already closed by the time my exception handler method is called.
While Initialising WebClient As mentioned in the code block, whenever a 5XX/4XX Error occurs, we can throw a user defined exception, and then execute error handling logic based on those user defined exceptions. Once this error Handler is defined, we can add it in the WebClient Initialisation.
public interface RequestBodyAdvice. Allows customizing the request before its body is read and converted into an Object and also allows for processing of the resulting Object before it is passed into a controller method as an @RequestBody or an HttpEntity method argument.
You should use org.springframework.http.server.reactive.ServerHttpRequest
, because:
HttpServletRequest
and WebRequest
are Servlet/Spring MVC specificServerRequest
belongs to Spring WebFlux, but the functional variantIf you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With