@RestControllerAdvice配合@ExceptionHandler全局异常解决只能解决controller层的异常,但是对于Rpc调用时候的异常没办法解决,需要自己用AOP实现全局异常的处理
/** * @author wenxinyang * Rpc调用异常注解 */ @Documented @Target({ElementType.METHOD, ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) public @interface RpcServiceExCatch { } 对于开放出去的方法最好使用统一返回值包装,这样便于在异常捕获后返回值的封装
import com.alibaba.fastjson.JSON; import lombok.extern.slf4j.Slf4j; 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.aspectj.lang.reflect.MethodSignature; import org.springframework.stereotype.Component; /** * @author yangwenxin * @Date 2022/3/14 * @Description rpc 调用异常捕获处理 */ @Aspect @Component @Slf4j public class RpcServiceExCatchHandler { @Pointcut("@within(com.baizhiedu.rpc.RpcServiceExCatch)") protected void pointcut() { } @Around("pointcut()") public Object advice(ProceedingJoinPoint joinPoint) throws Throwable { Response response; try { return joinPoint.proceed(); } catch (Exception e) { log.error("RpcServiceExCatchHandler error {}", e); response = new Response(1001,e.getMessage()); // 项目中我的方法返回值为Response及其子类 所以就先序列化后再反序列化成对应返回值类型 , // 最好要统一返回值,规范并且方便捕获异常后的统一返回 MethodSignature methodSignature = (MethodSignature)joinPoint.getSignature(); Class<?> returnType = methodSignature.getReturnType(); return JSON.parseObject(JSON.toJSONString(response),returnType); } } } ps: 在Java中子类可以强转成父类,但父类不能强转成子类,如果某种场景下需要这样操作时候,可以先将父类序列化然后在反序列化成子类