| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- package cn.com.goldenwater.dcproj.target;
- import cn.com.goldenwater.core.web.BaseResponse;
- import cn.com.goldenwater.dcproj.constValue.HttpStatus;
- import cn.com.goldenwater.dcproj.service.ErrorService;
- import cn.com.goldenwater.target.CheckException;
- import org.aspectj.lang.ProceedingJoinPoint;
- import org.aspectj.lang.annotation.Around;
- import org.aspectj.lang.annotation.Aspect;
- import org.aspectj.lang.annotation.Pointcut;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.stereotype.Component;
- import org.springframework.web.context.request.RequestContextHolder;
- import org.springframework.web.context.request.ServletRequestAttributes;
- import javax.servlet.http.HttpServletRequest;
- import java.util.Optional;
- @Aspect //切面
- //@Order(3) //执行顺序
- @Component
- public class ExecutionTimeRecord {
- private Logger logger = LoggerFactory.getLogger(getClass());
- @Autowired
- private ErrorService errorService;
- @Value("${intercept.enable}")
- private boolean interceptEnable;
- /**
- * 需要排除部分生成模板的接口
- */
- @Pointcut("execution(* cn.com.goldenwater.dcproj.controller..*(..)) && " +
- "!execution(* cn.com.goldenwater.dcproj.controller.index..*(..)) && " +
- "!execution(* cn.com.goldenwater.dcproj.controller.sso..*(..)) && " +
- "!execution(* cn.com.goldenwater.dcproj.controller.ducha..*(..)) && " +
- "!execution(* cn.com.goldenwater.dcproj.task..*(..)) ")
- public void webException() {
- }
- @Around("webException()")
- public Object handlerControllerMethod(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
- // HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
- // try {
- long startTime = System.currentTimeMillis();
- Object o = proceedingJoinPoint.proceed();
- // if (o instanceof BaseResponse) {
- // BaseResponse resultBean = (BaseResponse) o;
- // ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
- // Optional.ofNullable(servletRequestAttributes).map(ServletRequestAttributes::getResponse).ifPresent(response -> {
- // if (resultBean.getCode() != null) {
- // response.setStatus(resultBean.getCode());
- // } else if (!resultBean.getSuccess()) {
- // response.setStatus(HttpStatus.BAD_REQUEST);
- // }
- // });
- // }
- long endTime = System.currentTimeMillis() - startTime;
- logger.info("方法执行耗时(ms):" + (endTime));
- // } catch (Throwable e) {
- // logger.error("异常:", e);
- // resultBean = handlerException(e);
- // if (interceptEnable) {
- // errorService.addThrowable(e, request);
- // }
- // }
- return o;
- }
- /**
- * 这个是判断异常的类型
- *
- * @param throwable
- * @return
- */
- private BaseResponse<?> handlerException(Throwable throwable) {
- BaseResponse<?> resultBean = new BaseResponse();
- HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
- if (throwable instanceof CheckException) {
- resultBean.setMessage(throwable.getMessage());
- resultBean.setCode(4001);
- } else if (throwable instanceof IllegalArgumentException) {
- resultBean.setMessage("系统检查异常,请联系管理员");
- resultBean.setCode(4002);
- } else {
- resultBean.setMessage("系统检查异常,请联系管理员");
- resultBean.setCode(5001);
- }
- resultBean.setPath("");
- resultBean.setThrowable("EXCEPTION");
- if (!interceptEnable) {
- resultBean.setPath(request.getServletPath());
- resultBean.setThrowable(throwable.getMessage());
- }
- resultBean.setSuccess(false);
- return resultBean;
- }
- }
|