From 1bfcd783b625a373114cd6284bc1747134be370f Mon Sep 17 00:00:00 2001 From: yangyufu Date: Tue, 16 Jun 2026 11:16:40 +0800 Subject: [PATCH] =?UTF-8?q?fix(logging):=20=E4=BC=98=E5=8C=96API=E6=97=A5?= =?UTF-8?q?=E5=BF=97=E8=AE=B0=E5=BD=95=E9=80=BB=E8=BE=91=E5=B9=B6=E6=94=B9?= =?UTF-8?q?=E8=BF=9B=E5=BC=82=E5=B8=B8=E5=A4=84=E7=90=86=20-=20=E9=87=8D?= =?UTF-8?q?=E6=9E=84=E5=93=8D=E5=BA=94=E7=BB=93=E6=9E=9C=E8=A7=A3=E6=9E=90?= =?UTF-8?q?=E9=80=BB=E8=BE=91=EF=BC=8C=E6=94=AF=E6=8C=81=E5=A4=9A=E7=A7=8D?= =?UTF-8?q?=E7=8A=B6=E6=80=81=E5=AD=97=E6=AE=B5=E5=88=A4=E6=96=AD=20-=20?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=AF=B9=E7=A9=BA=E5=93=8D=E5=BA=94=E5=92=8C?= =?UTF-8?q?=E5=BC=82=E5=B8=B8=E5=93=8D=E5=BA=94=E7=9A=84=E6=9B=B4=E5=AE=8C?= =?UTF-8?q?=E5=96=84=E5=A4=84=E7=90=86=20-=20=E6=94=B9=E8=BF=9B=E5=BC=82?= =?UTF-8?q?=E5=B8=B8=E6=8D=95=E8=8E=B7=E5=92=8C=E6=97=A5=E5=BF=97=E8=AE=B0?= =?UTF-8?q?=E5=BD=95=E6=9C=BA=E5=88=B6=20-=20=E5=B0=86MDM=E6=9C=8D?= =?UTF-8?q?=E5=8A=A1=E4=B8=AD=E7=9A=84=E5=BC=82=E5=B8=B8=E6=8A=9B=E5=87=BA?= =?UTF-8?q?=E6=94=B9=E4=B8=BA=E8=BF=94=E5=9B=9E=E9=94=99=E8=AF=AF=E7=8A=B6?= =?UTF-8?q?=E6=80=81=E5=AF=B9=E8=B1=A1=20-=20=E4=BF=AE=E5=A4=8Dfinally?= =?UTF-8?q?=E5=9D=97=E4=B8=AD=E6=97=A5=E5=BF=97=E4=BF=9D=E5=AD=98=E5=8F=AF?= =?UTF-8?q?=E8=83=BD=E5=8F=91=E7=94=9F=E7=9A=84=E5=BC=82=E5=B8=B8=E5=BD=B1?= =?UTF-8?q?=E5=93=8D=E4=B8=BB=E4=B8=9A=E5=8A=A1=E6=B5=81=E7=A8=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../modules/logging/aspect/ApiLogAspect.java | 96 ++++++++++++------- .../mdm/service/impl/MdmToLmsServiceImpl.java | 8 +- 2 files changed, 68 insertions(+), 36 deletions(-) diff --git a/lms/nladmin-system/src/main/java/org/nl/modules/logging/aspect/ApiLogAspect.java b/lms/nladmin-system/src/main/java/org/nl/modules/logging/aspect/ApiLogAspect.java index 87b80906a..a8c8284dd 100644 --- a/lms/nladmin-system/src/main/java/org/nl/modules/logging/aspect/ApiLogAspect.java +++ b/lms/nladmin-system/src/main/java/org/nl/modules/logging/aspect/ApiLogAspect.java @@ -54,7 +54,8 @@ public class ApiLogAspect { logEntity.setBizDesc(apiLog.bizDesc()); logEntity.setTraceId(MDC.get("traceId")); logEntity.setCreateTime(DateUtil.now()); - + Object result = null; + Throwable businessException = null; try { if (attributes != null) { HttpServletRequest request = attributes.getRequest(); @@ -63,50 +64,77 @@ public class ApiLogAspect { logEntity.setRequestIp(getIpAddress(request)); logEntity.setRequestHeaders(JSONUtil.toJsonStr(getRequestHeaders(request))); } - MethodSignature signature = (MethodSignature) joinPoint.getSignature(); Method method = signature.getMethod(); Object[] args = joinPoint.getArgs(); logEntity.setRequestParams(buildRequestParams(method, args)); - - Object result = joinPoint.proceed(); - - - JSONObject jsonObject = JSONObject.parseObject(JSONUtil.toJsonStr(result)); - JSONObject jsonObject1 = jsonObject.getJSONObject("body"); - if(null == jsonObject1){ - logEntity.setResponseStatus(400); - logEntity.setStatus("FAIL"); - logEntity.setErrorMsg("返回信息为空,请确认!"); - logEntity.setResponseBody("返回信息为空,请确认!"); - }else{ - String rtype = jsonObject1.getString("RTYPE"); - if(rtype.equalsIgnoreCase("E")){ - logEntity.setResponseStatus(400); - logEntity.setStatus("FAIL"); - logEntity.setErrorMsg(JSONUtil.toJsonStr(result)); - logEntity.setResponseBody(JSONUtil.toJsonStr(result)); + result = joinPoint.proceed(); + try { + JSONObject jsonObject = JSONObject.parseObject(JSONUtil.toJsonStr(result)); + if(null != jsonObject && jsonObject.containsKey("status")){ + String status = jsonObject.getString("status"); + if ("E".equalsIgnoreCase(status)) { + logEntity.setResponseStatus(400); + logEntity.setStatus("FAIL"); + logEntity.setErrorMsg(JSONUtil.toJsonStr(result)); + logEntity.setResponseBody(JSONUtil.toJsonStr(result)); + } else { + logEntity.setResponseStatus(200); + logEntity.setStatus("SUCCESS"); + logEntity.setResponseBody(JSONUtil.toJsonStr(result)); + } }else{ - logEntity.setResponseStatus(200); - logEntity.setStatus("SUCCESS"); - logEntity.setResponseBody(JSONUtil.toJsonStr(result)); + JSONObject jsonObject1 = jsonObject != null ? jsonObject.getJSONObject("body") : null; + if (jsonObject1 == null) { + logEntity.setResponseStatus(400); + logEntity.setStatus("FAIL"); + logEntity.setErrorMsg("返回信息为空,请确认!"); + logEntity.setResponseBody(JSONUtil.toJsonStr(result)); + } else { + String key = ""; + if (jsonObject1.containsKey("RTYPE")) { + key = "RTYPE"; + } else if (jsonObject1.containsKey("TYPE")) { + key = "TYPE"; + } + String rtype = StrUtil.isNotBlank(key) ? jsonObject1.getString(key) : null; + if ("E".equalsIgnoreCase(rtype)) { + logEntity.setResponseStatus(400); + logEntity.setStatus("FAIL"); + logEntity.setErrorMsg(JSONUtil.toJsonStr(result)); + logEntity.setResponseBody(JSONUtil.toJsonStr(result)); + } else { + logEntity.setResponseStatus(200); + logEntity.setStatus("SUCCESS"); + logEntity.setResponseBody(JSONUtil.toJsonStr(result)); + } + } } + } catch (Exception e) { + log.error("解析响应结果失败,记录原始响应", e); + logEntity.setResponseStatus(200); + logEntity.setStatus("SUCCESS"); + logEntity.setResponseBody(JSONUtil.toJsonStr(result)); } - long costTime = System.currentTimeMillis() - startTime; - logEntity.setCostTime(costTime); - return result; - } catch (Throwable throwable) { + businessException = throwable; + logEntity.setStatus("FAIL"); + String errorMsg = throwable.getMessage(); + logEntity.setErrorMsg(errorMsg != null && errorMsg.length() > 2000 ? + errorMsg.substring(0, 2000) : errorMsg); + } finally { long costTime = System.currentTimeMillis() - startTime; logEntity.setCostTime(costTime); - logEntity.setStatus("FAIL"); - logEntity.setErrorMsg(throwable.getMessage()); - - throw throwable; - - } finally { - apiLogService.saveAsync(logEntity); + try { + apiLogService.saveAsync(logEntity); + } catch (Exception e) { + log.error("保存API日志失败,但不影响主业务", e); + } + if (businessException != null) { + throw businessException; + } } + return result; } private String getIpAddress(HttpServletRequest request) { diff --git a/lms/nladmin-system/src/main/java/org/nl/wms/ext/mdm/service/impl/MdmToLmsServiceImpl.java b/lms/nladmin-system/src/main/java/org/nl/wms/ext/mdm/service/impl/MdmToLmsServiceImpl.java index 8d6d073aa..0835476a4 100644 --- a/lms/nladmin-system/src/main/java/org/nl/wms/ext/mdm/service/impl/MdmToLmsServiceImpl.java +++ b/lms/nladmin-system/src/main/java/org/nl/wms/ext/mdm/service/impl/MdmToLmsServiceImpl.java @@ -250,7 +250,9 @@ public class MdmToLmsServiceImpl implements MdmToLmsService { if (ObjectUtil.isEmpty(material_jo)) { JSONObject unit = WQLObject.getWQLObject("md_pb_measureunit").query("unit_code = '" + meins + "' and is_delete ='0'").uniqueResult(0); if (ObjectUtil.isEmpty(unit)) { - throw new BadRequestException("MDM->未查询到相关计量单位,请进行维护!"); + commonResponseDto.setStatus("E"); + commonResponseDto.setMessage("MDM->未查询到相关计量单位,请进行维护!"); + return commonResponseDto; } material_jo = new JSONObject(); material_jo.put("material_id", mdId); @@ -268,7 +270,9 @@ public class MdmToLmsServiceImpl implements MdmToLmsService { } else { JSONObject unit = WQLObject.getWQLObject("md_pb_measureunit").query("unit_code = '" + meins + "' and is_delete ='0'").uniqueResult(0); if (ObjectUtil.isEmpty(unit)) { - throw new BadRequestException("MDM->未查询到相关计量单位,请进行维护!"); + commonResponseDto.setStatus("E"); + commonResponseDto.setMessage("MDM->未查询到相关计量单位,请进行维护!"); + return commonResponseDto; } material_jo.put("material_name", mdDescription); material_jo.put("base_unit_id", unit.getString("measure_unit_id"));