how to log response body in exception handler
need to log the response body in exception handler, but the response doesn't have a getBody method, search a lots, but have not found a solution for this need
Just need to log the response body of the case of exception, not for every request processing
Chat GPT gave this code, but there is no getBody(). Chat GPT keeping giving code using getBody()
exchange.getResponse().getBody() .map(dataBuffer -> { byte[] bytes = new byte[dataBuffer.readableByteCount()]; dataBuffer.read(bytes); // Assuming UTF-8 encoding, you can change it based on your application's encoding return new String(bytes, java.nio.charset.StandardCharsets.UTF_8); }) .doOnNext(responseBody -> { // Do something with the response body System.out.println("Response Body: " + responseBody); }) .subscribe();
the ServerHttpResponseDecorator is loging response body for every request processing, our need is to log the response body just in exception case
our exception logging standard has some required logging fields besides the logging response body in a same json logging message logged in the exception handler
Have you tried https://docs.spring.io/spring-framework/reference/web/webflux/reactive-spring.html#webflux-exception-handler
If you would like us to look at this issue, please provide the requested information. If the information is not provided within the next 7 days this issue will be closed.
Closing due to lack of requested feedback. If you would like us to look at this issue, please provide the requested information and we will re-open the issue.