Readable debug logging for http requests with spring webclient

后端 未结 3 1002
死守一世寂寞
死守一世寂寞 2021-02-15 12:31

I\'m using Spring reactive WebClient for sending requests to a http server. Inorder to view the underlying request & response that\'s being sent, I enabled debug logging for

3条回答
  •  广开言路
    2021-02-15 13:26

    You can do it with doOnNext(), if you use DataBuffer as your reader:

    public Mono selectByPost(ServerRequest request) {
      Flux requestBodyFlux = request.bodyToFlux(DataBuffer.class)
        .doOnNext(dataBuffer -> {
          if (debug ) {
            log.debug(new String(dataBuffer.asByteBuffer().array()));
          }
          Scannable.from(dataBuffer).tags().forEach(System.out::println);
        });
    }
    

    This is probably not the best way to do it, it would of course be a nice feature, if netty would provide different ways of logging the payload. Hex does have its benefits, depending on what you need to debug.

提交回复
热议问题