如何在Spring WebClient/DataBuffer中拦截HTTP响应流量? [英] How to intercept http response traffic in Spring WebClient / DataBuffer?
问题描述
在将WebClient
XML响应从字节转换为DTO之前,我该如何拦截?
我尝试添加exchangeStrategy
,但是如何将DataBuffer
转换为String,然后仍然调用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));
}
问题:由于我已经阅读了DataBuffer
,因此不再执行map()
.我怎么能多次阅读?
到目前为止,我想出了以下解决方案.它可以工作,但是我仍然愿意进行改进,因为我什至不知道我在这里是否做得正确.
@Override
public Flux<Object> decode(Publisher<DataBuffer> inputStream, ResolvableType elementType, MimeType mimeType, Map<String, Object> hints) {
return DataBufferUtils.join(inputStream)
.doOnNext(buf -> LOGGER.info(StandardCharsets.UTF_8.decode(buf.asByteBuffer()).toString()))
.flatMapMany(buf -> super.decode(Mono.fromSupplier(() -> buf), elementType, mimeType, hints));
}
How can I intercept WebClient
XML responses before they are converted from bytes to DTO?
I tried adding an exchangeStrategy
, but how could I convert DataBuffer
to String, and afterwards still invoke the super.decode()
method?
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);
}
}));
I succeeded as follows, but I don't know if that is the correct solution? Especially returning an empty collection inside the flatMapInterable()
feels wrong, but I did not find another way to make it work.
@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));
}
Problem: map()
is not executed anymore due to the fact that I already read the DataBuffer
. How could I read it multiple times?
So far I came up with the following solution. It works, but I'm still open for improvements as I don't even know if I'm doing this correctly here.
@Override
public Flux<Object> decode(Publisher<DataBuffer> inputStream, ResolvableType elementType, MimeType mimeType, Map<String, Object> hints) {
return DataBufferUtils.join(inputStream)
.doOnNext(buf -> LOGGER.info(StandardCharsets.UTF_8.decode(buf.asByteBuffer()).toString()))
.flatMapMany(buf -> super.decode(Mono.fromSupplier(() -> buf), elementType, mimeType, hints));
}
这篇关于如何在Spring WebClient/DataBuffer中拦截HTTP响应流量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!