I developed a simple org.springframework.web.server.WebFilter, in a reactive application based on Spring Boot 2.3.4, to log the HTTP request header and response details.
The filter method is basically:
public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
System.out.println("Status=" + exchange.getRequest().getHeader()); //returns value
System.out.println("Status=" + exchange.getRequest().getMethodValue()); //returns value
return chain.filter(exchange).doAfterTerminate(() -> {
System.out.println("Status=" + exchange.getRequest().getHeader()); //returns null
System.out.println("Status=" + exchange.getRequest().getMethodValue()); //returns null
});
}
For many REST controller methods the logged header is null and methodvalue also null after the controller method gets executed.
Note: I am able to get the value from exchange before .doAfterTerminate . Note: I dont have options to modify the controller code to return ResponseEntity<>.
Is there any way to fix this issue without changing the controller return type.?
Comment From: bclozel
This is the expected behavior here, as the doAfterTerminate operator is called after success or failure. At this point, the exchange (request and response) are gone and associated resources can be recycled by the underlying web server.
You should try instead to use a combination of doOnSuccess and doOnError.