1.使用spring boot实现一个拦截器
1、引入依赖:
<
dependency
>
<
groupId
>org.springframework.boot</
groupId
>
<
artifactId
>spring-boot-starter-aop</
artifactId
>
</
dependency
>
/**
* 拦截器:记录O2O调用接口记录
*/
@Aspect
@Component
public class O2oInterfaceInterceptor {
@Autowired
private SysCallInterfaceLogRepository sysCallInterfaceLogRepository;
@Autowired
private SysEmailService sysEmailService;
@Autowired
private SysPropertyService sysPropertyService;
private static Logger logger = LoggerFactory.getLogger(O2oInterfaceInterceptor.class);
/**
* 定义拦截规则:拦截com.ctop.wms.interfaces.ActivitiInterfaces包下面的所有类中,有@RequestMapping注解的方法。
*/
@Pointcut("execution(* com.ctop.wms.interfaces.ActivitiInterfaces.*(..)) and @annotation(org.springframework.web.bind.annotation.RequestMapping)")
public void controllerMethodPointcut() {}
@Around("controllerMethodPointcut()")
public Object controllerMethodPointcutInterceptor(ProceedingJoinPoint pjp) {
return Interceptor(pjp);
}
/**
* 拦截器具体实现
* @param pjp
* @return JsonResult(被拦截方法的执行结果)
*/
public Object Interceptor(ProceedingJoinPoint pjp){
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.getRequest();
MethodSignature signature = (MethodSignature) pjp.getSignature();
Method method = signature.getMethod(); //获取被拦截的方法
String methodName = method.getName(); //获取被拦截的方法名
String ip = request.getRemoteAddr();
String url = request.getRequestURL().toString();
logger.info("被调接口,请求开始----------------------------");
logger.info("ip : " + request.getRemoteAddr());
logger.info("url : " + request.getRequestURL().toString());
logger.info("methodName : " + methodName);
Object result = "";
String param = Arrays.toString(pjp.getArgs());
logger.info("param : " + param);
SysCallInterfaceLog log = new SysCallInterfaceLog();
log.setExt1("called");// 被叫
log.setRequestIp(ip);
log.setRequestMethod(methodName);
log.setRequestParam(param);
log.setRequestUrl(url);
try {
// 一切正常的情况下,继续执行被拦截的方法
result = pjp.proceed();
String resultStr = "";
if(result != null) {
if(result instanceof String) {
resultStr = (String) result;
} else {
Gson gson = new Gson();
resultStr = gson.toJson(result);
}
}
log.setResult(resultStr);
logger.info("result : " + resultStr);
logger.info("请求结束,请求成功");
// 记录结果到数据库
log.setSuccess("success");
log = sysCallInterfaceLogRepository.save(log);
} catch (Throwable e) {
logger.info("请求结束,请求失败");
// 记录结果到数据库, 并发送邮件
log.setSuccess("fail");
Gson gson = new Gson();
String exceptionStr = gson.toJson(e);
log.setResult(exceptionStr);
SysProperty sysPropertyName= sysPropertyService.getSysProperty("o2o.wms.log.name");
SysProperty sysPropertyEmail= sysPropertyService.getSysProperty("o2o.wms.log.email");
log = sysCallInterfaceLogRepository.save(log);
SysEmailDto sysEmailDto = new SysEmailDto();
List<SysEmailInfoDto> sysEmailInfoDtoLs = new ArrayList<SysEmailInfoDto>();
SysEmailInfoDto dtoDetails = new SysEmailInfoDto();
if(sysPropertyName !=null && sysPropertyEmail !=null){
dtoDetails.setReceiverEmail(sysPropertyEmail.getPropValue());
dtoDetails.setReceiverName(sysPropertyName.getPropValue());
}
sysEmailDto.setTitle("O2O调用WMS接口失败日志!");
sysEmailDto.setContent("调用:"+url+"失败!sys_Call_Interface_Log.Scil_Uuid="+log.getScilUuid()+",
请求参数:"+param+",
调用结果:"+exceptionStr);
sysEmailInfoDtoLs.add(dtoDetails);
sysEmailDto.setSysEmailInfoDto(sysEmailInfoDtoLs);
try {
sysEmailService.addSysEmail(sysEmailDto);
} catch (Exception e1) {
e1.printStackTrace();
}
e.printStackTrace();
throw new BusinessException(e, null, null);
}
return result;
}
}