Merge pull request '限制仅当异常响应时才对错误信息进行标准化处理' (#832) from otto/microservices:third-party-tool-forward into third-party-tool-forward

This commit is contained in:
otto 2025-02-25 10:38:09 +08:00
commit 103beae4d8
1 changed files with 14 additions and 21 deletions

View File

@ -12,7 +12,6 @@ import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.core.io.buffer.DataBufferFactory; import org.springframework.core.io.buffer.DataBufferFactory;
import org.springframework.core.io.buffer.DataBufferUtils; import org.springframework.core.io.buffer.DataBufferUtils;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.server.reactive.ServerHttpRequest; import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse; import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.http.server.reactive.ServerHttpResponseDecorator; import org.springframework.http.server.reactive.ServerHttpResponseDecorator;
@ -40,35 +39,29 @@ public class GlobalResponseFilter implements GlobalFilter, Ordered {
ServerHttpResponseDecorator decoratedResponse = new ServerHttpResponseDecorator(originalResponse) { ServerHttpResponseDecorator decoratedResponse = new ServerHttpResponseDecorator(originalResponse) {
@Override @Override
public Mono<Void> writeWith(Publisher<? extends DataBuffer> body) { public Mono<Void> writeWith(Publisher<? extends DataBuffer> body) {
MediaType contentType = getHeaders().getContentType();
HttpStatus status = getStatusCode() != null ? getStatusCode() : HttpStatus.INTERNAL_SERVER_ERROR; HttpStatus status = getStatusCode() != null ? getStatusCode() : HttpStatus.INTERNAL_SERVER_ERROR;
if (!status.isError()) {
// 排除无内容响应和二进制类型
if (status == HttpStatus.NO_CONTENT ||
(contentType != null && (contentType.includes(MediaType.APPLICATION_OCTET_STREAM) ||
contentType.includes(MediaType.MULTIPART_FORM_DATA)))) {
return super.writeWith(body); return super.writeWith(body);
} }
// 保留原始状态码
originalResponse.setStatusCode(HttpStatus.OK);
getHeaders().setContentType(MediaType.APPLICATION_JSON);
return Flux.from(body).<String>handle((dataBuffer, synchronousSink) -> { return Flux.from(body).<String>handle((dataBuffer, synchronousSink) -> {
byte[] bytes = new byte[dataBuffer.readableByteCount()]; if (status.isError()) {
dataBuffer.read(bytes); byte[] bytes = new byte[dataBuffer.readableByteCount()];
//释放掉内存 dataBuffer.read(bytes);
DataBufferUtils.release(dataBuffer); //释放掉内存
String result = new String(bytes, StandardCharsets.UTF_8); DataBufferUtils.release(dataBuffer);
if (!JSON.isValid(result)) { String result = new String(bytes, StandardCharsets.UTF_8);
synchronousSink.next(result); if (!JSON.isValid(result)) {
} else { synchronousSink.next(result);
synchronousSink.complete(); return;
}
} }
synchronousSink.complete();
}).flatMap(resBody -> { }).flatMap(resBody -> {
// 保留原始状态码
originalResponse.setStatusCode(HttpStatus.OK);
byte[] bytes = JSON.toJSONBytes(AjaxResult.error(resBody)); byte[] bytes = JSON.toJSONBytes(AjaxResult.error(resBody));
getHeaders().setContentLength(bytes.length); getHeaders().setContentLength(bytes.length);
DataBuffer buffer = bufferFactory.wrap(bytes); DataBuffer buffer = bufferFactory.wrap(bytes);
return super.writeWith(Mono.just(buffer)); return super.writeWith(Mono.just(buffer));