Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring-webflux, how to get the request as parameter in @ExceptionHandler

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 ?

like image 743
gervais.b Avatar asked Sep 19 '18 15:09

gervais.b


People also ask

How do I get a request body in exception handler?

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.

How does WebClient handle error response?

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.

What is RequestBodyAdvice?

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.


1 Answers

You should use org.springframework.http.server.reactive.ServerHttpRequest, because:

  • HttpServletRequest and WebRequest are Servlet/Spring MVC specific
  • ServerRequest belongs to Spring WebFlux, but the functional variant
like image 196
Brian Clozel Avatar answered Oct 23 '22 16:10

Brian Clozel