0

将请求从 DTO 转换为 JSON 字符串后,如何拦截WebFilter它们?

当然我可以添加一个ExchangeFilterFunction,但clientRequest.body()只显示我的输入对象,而不是转换后的 json 字符串:

WebClient.builder().defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE).filter(logRequest()).build();

private ExchangeFilterFunction logRequest() {
    return ExchangeFilterFunction.ofRequestProcessor(clientRequest -> {
        LOGGER.info(clientRequest.body()); //body is MyRequest.class
        return Mono.just(clientRequest);
    });
}

MyRequestDto dto;
client.post().uri(url).syncBody(dto).retrieve().bodyToMono(MyResponseDto.class).block();

因此,我正在寻找一种在将传出请求从 dto 转换为 json 字符串后拦截它们的方法。

反过来,在响应从 json 转换为 dto之前拦截响应。

4

1 回答 1

1

JacksonEncoder将数据String作为一个字节写入。DataBuffer拦截请求的一种方法是覆盖 JSON 编码器,并DataBuffer直接访问或将其解释为String.

Consumer<ClientCodecConfigurer> consumer = configurer ->
        configurer.defaultCodecs().jackson2JsonEncoder(new Jackson2JsonEncoder() {
            @Override
            public Flux<DataBuffer> encode(Publisher<?> inputStream, DataBufferFactory bufferFactory, ResolvableType elementType, @Nullable MimeType mimeType, @Nullable Map<String, Object> hints) {
                return super.encode(inputStream, bufferFactory, elementType, mimeType, hints)
                        .doOnNext(dataBuffer -> System.out.println(StandardCharsets.UTF_8.decode(dataBuffer.asByteBuffer()).toString()));
            }
        });

WebClient webClient = WebClient.builder()
        .exchangeStrategies(ExchangeStrategies.builder().codecs(consumer).build())
        .build();

如果目的只是记录传出消息正文,请参阅以下问题:如何记录 Spring 5 WebClient 调用

于 2019-07-11T20:11:24.587 回答