解决SpringAOP切面中around环绕方法出现空指针异常
这两天想给接口加一些出入参的数据入库操作,于是就加了个AOP,但是加了之后所有被拦截的接口返回值都变成了NULL。
下面标红的地方是解决办法,之前我是void,没有返回。
记录下,以便后面再遇到这个问题
一、环绕
/** * aop拦截,日志打印处理 */ @Slf4j @Component @Aspect public class RsetAPIEsignAspectj { @Autowired private ZyEsignApiMsgRecordService zyEsignApiMsgRecordService; @Pointcut("execution(* com.zy.esign.api.service.*.*(..))") public void resetAPIPointcut() { } @Around("resetAPIPointcut()") public Object around(ProceedingJoinPoint point) throws Throwable { try { //request HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); //ip地址 String ipAddr = IPUtil.getIpAddr(request); //入参 String reqJson = methodBefore(point); JSONObject reqResult = JSONObject.parseObject(reqJson); String source = String.valueOf(reqResult.get("source")); //出参 String resJson = JSONObject.toJSONString(point.proceed()); JSONObject resResult = JSONObject.parseObject(resJson); String retCode = String.valueOf(resResult.get("code")); String retMsg = String.valueOf(resResult.get("message")); zyEsignApiMsgRecordService.save(String.valueOf(point.getSignature().getName()), ZyEsignApiMsgRecordService.REQ, reqJson, resJson, retCode, retMsg, StringUtils.isNotBlank(source) ? source : "", null, ipAddr); } catch (Exception e) { log.error("E签宝aop日志记录异常1:", e); } finally { return point.proceed(); } } public String methodBefore(JoinPoint joinPoint) { Map<String, Object> paramMap = new HashMap<>(); Object[] objs = joinPoint.getArgs(); String[] argNames = ((MethodSignature) joinPoint.getSignature()).getParameterNames(); // 参数名 for (int i = 0; i < objs.length; i++) { if (!(objs[i] instanceof ExtendedServletRequestDataBinder) && !(objs[i] instanceof HttpServletResponseWrapper)) { paramMap.put(argNames[i], objs[i]); } } return JSONObject.toJSONString(paramMap); } }
二、方法开始、结束打印日志
/** * aop拦截,日志打印处理 */ @Component @Aspect public class RsetAPIAspectj { protected final Logger logger = LogManager.getLogger(this.getClass()); @Pointcut("execution(* com.zy.esign.controller.*.*(..))") public void resetAPIPointcut() { } @Before(value = "resetAPIPointcut()") public void restAPIBefore(JoinPoint jp) { String httpReqParam = null; String ip = null; HttpServletRequest request = null; try { StringBuilder sb = new StringBuilder("====调用") .append(jp.getSignature().toString()).append("方法-开始:"); for (Object obj : jp.getArgs()) { if (obj instanceof HttpServletRequest) { request = (HttpServletRequest) obj; ip = "ip:[" + request.getRemoteAddr() + "]"; httpReqParam = "httpReqParam:" + JSONObject.toJSONString((request).getParameterMap()); sb.append(ip).append(httpReqParam); } else if (obj instanceof HttpServletResponse) { } else { sb.append(JSONObject.toJSONString(obj)); } } logger.info(sb.toString()); } catch (Exception e) { } } @AfterReturning(pointcut = "resetAPIPointcut()", returning = "returnVal") public void restAPIAfter(JoinPoint jp, Object returnVal) { try { StringBuilder sb = new StringBuilder("====调用") .append(jp.getSignature().toString()).append("方法-结束:"); sb.append(JSONObject.toJSONString(returnVal)); logger.info(sb.toString()); } catch (Exception e) { } }