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"));