b94f8a5e4f21bd0a2151ff17101848d1995bd21f.svn-base 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. package cn.com.goldenwater.dcproj.target;
  2. import cn.com.goldenwater.core.web.BaseResponse;
  3. import cn.com.goldenwater.dcproj.constValue.HttpStatus;
  4. import cn.com.goldenwater.dcproj.service.ErrorService;
  5. import cn.com.goldenwater.target.CheckException;
  6. import org.aspectj.lang.ProceedingJoinPoint;
  7. import org.aspectj.lang.annotation.Around;
  8. import org.aspectj.lang.annotation.Aspect;
  9. import org.aspectj.lang.annotation.Pointcut;
  10. import org.slf4j.Logger;
  11. import org.slf4j.LoggerFactory;
  12. import org.springframework.beans.factory.annotation.Autowired;
  13. import org.springframework.beans.factory.annotation.Value;
  14. import org.springframework.stereotype.Component;
  15. import org.springframework.web.context.request.RequestContextHolder;
  16. import org.springframework.web.context.request.ServletRequestAttributes;
  17. import javax.servlet.http.HttpServletRequest;
  18. import java.util.Optional;
  19. @Aspect //切面
  20. //@Order(3) //执行顺序
  21. @Component
  22. public class ExecutionTimeRecord {
  23. private Logger logger = LoggerFactory.getLogger(getClass());
  24. @Autowired
  25. private ErrorService errorService;
  26. @Value("${intercept.enable}")
  27. private boolean interceptEnable;
  28. /**
  29. * 需要排除部分生成模板的接口
  30. */
  31. @Pointcut("execution(* cn.com.goldenwater.dcproj.controller..*(..)) && " +
  32. "!execution(* cn.com.goldenwater.dcproj.controller.index..*(..)) && " +
  33. "!execution(* cn.com.goldenwater.dcproj.controller.sso..*(..)) && " +
  34. "!execution(* cn.com.goldenwater.dcproj.controller.ducha..*(..)) && " +
  35. "!execution(* cn.com.goldenwater.dcproj.task..*(..)) ")
  36. public void webException() {
  37. }
  38. @Around("webException()")
  39. public Object handlerControllerMethod(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
  40. // HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
  41. // try {
  42. long startTime = System.currentTimeMillis();
  43. Object o = proceedingJoinPoint.proceed();
  44. // if (o instanceof BaseResponse) {
  45. // BaseResponse resultBean = (BaseResponse) o;
  46. // ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
  47. // Optional.ofNullable(servletRequestAttributes).map(ServletRequestAttributes::getResponse).ifPresent(response -> {
  48. // if (resultBean.getCode() != null) {
  49. // response.setStatus(resultBean.getCode());
  50. // } else if (!resultBean.getSuccess()) {
  51. // response.setStatus(HttpStatus.BAD_REQUEST);
  52. // }
  53. // });
  54. // }
  55. long endTime = System.currentTimeMillis() - startTime;
  56. logger.info("方法执行耗时(ms):" + (endTime));
  57. // } catch (Throwable e) {
  58. // logger.error("异常:", e);
  59. // resultBean = handlerException(e);
  60. // if (interceptEnable) {
  61. // errorService.addThrowable(e, request);
  62. // }
  63. // }
  64. return o;
  65. }
  66. /**
  67. * 这个是判断异常的类型
  68. *
  69. * @param throwable
  70. * @return
  71. */
  72. private BaseResponse<?> handlerException(Throwable throwable) {
  73. BaseResponse<?> resultBean = new BaseResponse();
  74. HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
  75. if (throwable instanceof CheckException) {
  76. resultBean.setMessage(throwable.getMessage());
  77. resultBean.setCode(4001);
  78. } else if (throwable instanceof IllegalArgumentException) {
  79. resultBean.setMessage("系统检查异常,请联系管理员");
  80. resultBean.setCode(4002);
  81. } else {
  82. resultBean.setMessage("系统检查异常,请联系管理员");
  83. resultBean.setCode(5001);
  84. }
  85. resultBean.setPath("");
  86. resultBean.setThrowable("EXCEPTION");
  87. if (!interceptEnable) {
  88. resultBean.setPath(request.getServletPath());
  89. resultBean.setThrowable(throwable.getMessage());
  90. }
  91. resultBean.setSuccess(false);
  92. return resultBean;
  93. }
  94. }