修改返回值日志

This commit is contained in:
2025-09-08 17:43:14 +08:00
parent bb92857db9
commit 8b5648a3dc

View File

@ -94,7 +94,7 @@ public class LoggingGlobalFilter implements GlobalFilter, Ordered {
@Override
public ServerHttpResponse getResponse() {
ServerHttpResponse originalResponse = super.getResponse();
DataBufferFactory bufferFactory = originalResponse.bufferFactory();
// DataBufferFactory bufferFactory = originalResponse.bufferFactory();
return new ServerHttpResponseDecorator(originalResponse) {
@Override
@ -102,12 +102,15 @@ public class LoggingGlobalFilter implements GlobalFilter, Ordered {
if (body instanceof Flux) {
Flux<? extends DataBuffer> flux = (Flux<? extends DataBuffer>) body;
return super.writeWith(flux.doOnNext(dataBuffer -> {
// 读取数据内容用于日志记录
byte[] bytes = new byte[dataBuffer.readableByteCount()];
dataBuffer.read(bytes);
String bodyContent = new String(bytes, StandardCharsets.UTF_8);
responseBodyBuilder.append(bodyContent);
DataBufferUtils.release(dataBuffer);
}).map(bytes -> bufferFactory.wrap((ByteBuffer) bytes)));
// 重置读取位置,以便后续处理可以正常读取
dataBuffer.readPosition(0);
}));
}
return super.writeWith(body);
}