package com.pt.modules.log;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.pt.modules.cfca.util.OutLogTextUtil;
import com.pt.modules.contract.rmtcontractlog.dto.RmtContractLogDTO;
import com.pt.modules.contract.rmtcontractlog.service.RmtContractLogService;
import com.pt.modules.contract.utils.Customannotations.OperationDescription;
@Aspect
@Scope("prototype")
public class LoanLogAspect {
private Logger logger = LoggerFactory.getLogger(LoanLogAspect.class);
// 缓存有@OperationDescription方法参数名称
private static Map<String, String[]> parameterNameCaches = new ConcurrentHashMap<String, String[]>();
/**
* 接口请求记录实例
*/
@Autowired
@Qualifier("com.pt.modules.contract.rmtcontractlog.service.RmtContractLogService")
private RmtContractLogService rmtContractLogService;
@Around("execution(* com.pt.modules.*.rest.*Rest*.*(..)) && @annotation(annotation)")
public Object advice(ProceedingJoinPoint joinPoint, OperationDescription annotation) throws Throwable{
String descption = annotation.description();
String entityType = annotation.entityType();
String reqContent = JSON.toJSONString(joinPoint.getArgs());
logger.info("\n\n"+OutLogTextUtil.outLogText("--接口名称----"+entityType+"-----操作动作:----"+descption+"---拦截器-接收到请求报文------------"+ reqContent));
Object result = joinPoint.proceed();
JSONObject response = JSON.parseObject(JSON.toJSONString(result));
response = response.getJSONObject("body");
String retcode = response.getString("retCode");
String errorDesc = response.getString("errorDesc");
RmtContractLogDTO dto = new RmtContractLogDTO();
dto.setErrMessage(errorDesc);
if(retcode!=""&&retcode!=null&&"302".equals(retcode)){
dto.setState("0");//失败状态
}else{
dto.setState("1");//成功状态
dto.setErrMessage(null);
}
this.saveRmtContractLog(dto, reqContent, "外部系统调用", entityType, descption,response.toString());
logger.info("\n\n"+OutLogTextUtil.outLogText("--接口名称----"+entityType+"-----操作动作:----"+descption+"---拦截器-返回接口报文------------"+ response.toString()));
return result;
}
public void saveRmtContractLog(RmtContractLogDTO dto,String requestJson,String Systemsource ,
String interfaceName,String reqmapping,String responseJson) throws Exception{
dto.setSystemsource(Systemsource);
dto.setInterfaceName(interfaceName);
dto.setRequestJson(requestJson);
dto.setReqmapping(reqmapping);
dto.setResponseJson(responseJson);
rmtContractLogService.insertRmtContractLog(dto);
}
}
package com.pt.modules.contract.utils.Customannotations;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
public @interface OperationDescription {
/**
* 方法描述
* @return
*/
public String description() default "no description";
/**
* 操作
* @return
*/
public String entityType() default "";
}
<aop:aspectj-autoproxy proxy-target-class="true" />
<bean id="loanLogAspect" class="com.pt.modules.log.LoanLogAspect" />