基于spring aop的dubbo异常统一处理

来源:互联网 发布:网络咨询医生在线 编辑:程序博客网 时间:2024/06/12 17:19

dubbo统一异常处理,调用方只显示封装后的异常。

1、返回封装后的Exception

2、返回封装后的统一返回信息

import org.aspectj.lang.annotation.AfterThrowing;import org.aspectj.lang.annotation.Aspect;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.stereotype.Component;import com.midea.jr.commons.utils.BeanUtils;import com.midea.jr.efc.edp.common.bean.Response;import com.midea.jr.efc.edp.common.enums.ResponseCodeEnum;import com.midea.jr.efc.edp.common.exception.BizException;/** * <p>Project: <p> * <p>Module: <p> * <p>Description: <p> * * @author wangzy25 * @date 2017年6月12日 上午10:07:58 */@Component@Aspectpublic class DubboExceptionInterceptor {private static final Logger LOGGER = LoggerFactory.getLogger(DubboExceptionInterceptor.class);@AfterThrowing(throwing="ex",pointcut="execution(* com.midea.jr.efc.edp.api.service.**.*(..))")    public BizException handle(Exception ex){LOGGER.error("Intercept dubbo exception:{}", ex.getMessage(), ex);if(ex instanceof BizException){throw (BizException)ex;}//如果是非包装异常,则包装成统一的内部异常        throw new BizException(ResponseCodeEnum.EXCEPTION_INTERAL);    }@AfterThrowing(throwing="ex",pointcut="execution(* com.midea.jr.efc.edp.api.service.**.*(..))")    public Response<?> handle(Exception ex){Response<?> response = new Response<>();LOGGER.error("intercept ServiceException:{}", ex.getMessage(), ex);if(ex instanceof BizException){BeanUtils.copyProperties( ex, response );return response;}//如果是非包装异常,则包装成统一的内部异常        return new Response<>(ResponseCodeEnum.EXCEPTION_INTERAL);    }}