How to log request and response bodies in Spring WebFlux

前端 未结 9 1154
一向
一向 2020-11-27 16:41

I want to have centralised logging for requests and responses in my REST API on Spring WebFlux with Kotlin. So far I\'ve tried this approaches

@Bean
fun apiR         


        
9条回答
  •  日久生厌
    2020-11-27 17:25

    Assuming we are dealing with a simple JSON or XML response, if debug level for corresponding loggers is not sufficient for some reason, one can use string representation before transforming it to object:

    Mono mono = WebClient.create()
                                   .post()
                                   .body(Mono.just(request), Request.class)
                                   .retrieve()
                                   .bodyToMono(String.class)
                                   .doOnNext(this::sideEffectWithResponseAsString)
                                   .map(this::transformToResponse);
    

    the following are the side-effect and transformation methods:

    private void sideEffectWithResponseAsString(String response) { ... }
    private Response transformToResponse(String response) { /*use Jackson or JAXB*/ }    
    

提交回复
热议问题