Merge pull request '当Nacos返回异常状态码时,且body数据类型为JSON时,从JSON中取出message字段作为异常响应信息,若不存在该字段则返回默认的错误信息' (#835) from otto/microservices:third-party-tool-forward into third-party-tool-forward

This commit is contained in:
otto 2025-03-07 08:52:29 +08:00
commit 05533c9475
1 changed files with 15 additions and 6 deletions

View File

@ -76,17 +76,26 @@ public class GlobalResponseFilter implements GlobalFilter, Ordered {
//释放掉内存 //释放掉内存
DataBufferUtils.release(dataBuffer); DataBufferUtils.release(dataBuffer);
String result = new String(bytes, StandardCharsets.UTF_8); String result = new String(bytes, StandardCharsets.UTF_8);
if (!JSON.isValid(result)) { synchronousSink.next(result);
synchronousSink.next(result); return;
return;
}
} }
synchronousSink.complete(); synchronousSink.complete();
}).flatMap(resBody -> { }).flatMap(resStr -> {
// 保留原始状态码 // 保留原始状态码
originalResponse.setStatusCode(HttpStatus.OK); originalResponse.setStatusCode(HttpStatus.OK);
byte[] bytes = JSON.toJSONBytes(AjaxResult.error(resBody)); String errMsg;
if (!JSON.isValid(resStr)) {
errMsg = resStr;
} else {
JSONObject resJson = JSONObject.parseObject(resStr);
if (resJson.containsKey("message")) {
errMsg = resJson.getString("message");
} else {
errMsg = ExceptionMsgConstants.SYSTEM_EXEC_ERROR;
}
}
byte[] bytes = JSON.toJSONBytes(AjaxResult.error(errMsg));
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));