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

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;

/**
 * 

Project:

*

Module:

*

Description:

* * @author wangzy25 * @date 2017年6月12日 上午10:07:58 */ @Component @Aspect public 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); } }



你可能感兴趣的:(金融技术,分布式技术,dubbo,rpc)