本文共 1605 字,大约阅读时间需要 5 分钟。
import com.sf.framework.exceptions.BaseServiceException;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.stereotype.Component;@Component@Aspectpublic class ServiceExceptionHandler { /** * 指定返回值为Response类型的Service */ @Pointcut(value = "execution(* com.xxx.service.MasterRestService.*(..))") private void servicePointcut() { // Do nothing just pointcut } /** * 异常处理切面 * 将异常包装为Response,避免dubbo进行包装 * * @param pjp 处理点 * @return 返回异常处理结果 */ @Around("servicePointcut()") public Object doAround(final ProceedingJoinPoint pjp) { final Logger logger = LoggerFactory.getLogger(pjp.getTarget().getClass()); final Object[] args = pjp.getArgs(); final String log = String.format("\n%s ****** with params: {}", pjp.toShortString()); try { logger.info(log, args); return pjp.proceed(); } catch (Throwable e) { logger.error(log, args); logger.error("\n*************Exception*************", e); if (e instanceof BaseServiceException) { BaseServiceException be = (BaseServiceException) e; return ResponseUtil.buildFailedRsp(be.getErrorCode(), be.getMessage(), VersionUtil.V_4_1); } else { return ResponseUtil.buildFailedRsp(ErrorCodes.OTHER_EXCEPTION_9999, "未知异常", VersionUtil.V_4_1); } } }}
转载地址:http://daiqi.baihongyu.com/