最近项目需要做一个controller层的aop,主要解决下面问题:
1.controller日志统一打印输出json格式,兼容json和velocity 。
2.项目异常处理
3.异常邮件发送
4.页面访问统计
主要思路使用aop实现,controller参数统一使用@RequestParam接收。
controller
@RequestMapping(name = "添加个人信息", value ="/addInfo", method = RequestMethod.POST) public String addInfo(@RequestParam("idFront") MultipartFile idFront, @RequestParam("idReverse") MultipartFile idReverse, @RequestParam("idNo") String idNo, @RequestParam("realName") String realName, @RequestParam("token") String token, HttpServletRequest request) throws Exception {
aop
@Aspect@Componentpublic class CotrollerLogAop { private final static Logger logger = LoggerFactory.getLogger(CotrollerLogAop.class); //请求信息缓存 private final static Map MethodInfo=new ConcurrentHashMap (); //项目名 @Value("${base.project.name}") private String projectName; //spring boot 错误页面uri @Value("${server.error.path:/error}") private String errorPath; //邮件发送 @Autowired private EmailClient emailClient; //页面统计 @Autowired private PageStatService pageStatService; //切面 @Pointcut("@annotation(org.springframework.web.bind.annotation.RequestMapping)") public void requestMapping() { } //增强 @Around("requestMapping()") public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable { long startTime=System.currentTimeMillis(); long endTime=0; Object[] objects = pjp.getArgs(); String userName = ""; String reqArray = ""; String respView = null; String respData = null; String operation = null; Object object = null; String ip = null; Boolean returnJson=false; String[] paramNames=null; String referer=null; String userAgent=null; boolean errorReq=false; try { HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); operation=request.getRequestURI(); referer=request.getHeader("Referer"); if(referer==null) referer=""; userAgent=request.getHeader("User-Agent"); if(userAgent==null) userAgent=""; Method method = ((MethodSignature) pjp.getSignature()).getMethod(); if (operation.equals(errorPath)) { errorReq=true; returnJson=method.getAnnotation(ResponseBody.class)!=null; }else{ Object [] info=getMethodInfo(operation, method); returnJson=(Boolean) info[0]; paramNames=(String [])info[1]; } //请求相关参数 ip = RequestParamUtil.getClientIpAddr(request); SessionUser securityUser = (SessionUser) request.getSession().getAttribute(Constants.SESSIONKEY); if (securityUser != null) { userName = securityUser.getShowName(); } reqArray = ArrayToJsonString(objects, paramNames); //执行该方法 object = pjp.proceed(); //响应相关参数 if (returnJson) { if(object instanceof String){ respData=(String)object; }else{ respData = JSON.toJSONString(object); } } else { if (object instanceof ModelAndView) { respView = ((ModelAndView) object).getViewName(); } else { respView = (String) object; } respData = RequestParamUtil.getResponseStr(request); } //spring boot 系统错误 if(errorReq){ pageStatService.addStat("/error"); HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse(); if(returnJson){ Map map = new HashMap (); ResponseEntity entity = null; if (response.getStatus() == 404) { map.put("code", MsgCode.ERROR); map.put("msg", "资源不存在!"); entity = new ResponseEntity