如何WebClient
在 XML 响应从字节转换为 DTO 之前拦截它们?
我尝试添加一个exchangeStrategy
,但我如何转换DataBuffer
为字符串,然后仍然调用该super.decode()
方法?
ExchangeStrategies.builder().codecs((configurer) -> {
configurer.defaultCodecs().jackson2JsonDecoder(new Jaxb2XmlDecoder() {
@Override
public Flux<Object> decode(Publisher<DataBuffer> inputStream, ResolvableType elementType, MimeType mimeType, Map<String, Object> hints) {
//TODO how to log the response as string content?
return super.decode(inputStream, elementType, mimeType, hints);
}
}));
我成功如下,但我不知道这是否是正确的解决方案?尤其是在里面返回一个空集合flatMapInterable()
感觉不对,但我没有找到另一种方法让它工作。
@Override
public Flux<Object> decode(Publisher<DataBuffer> inputStream, ResolvableType elementType, MimeType mimeType, Map<String, Object> hints) {
return DataBufferUtils.join(inputStream)
.flatMapIterable(buffer -> {
try {
LOGGER.info(StandardCharsets.UTF_8.decode(buffer.asByteBuffer()).toString());
return Collections.emptyList();
} finally {
DataBufferUtils.release(buffer);
}
})
.map(arg -> super.decode(inputStream, elementType, mimeType, hints));
}
问题:map()
由于我已经阅读了DataBuffer
. 我怎么能读好几遍?