切面+自定义注解处理,统一封Service返回结果
@Component
@Aspect
@Slf4j
public class ServiceAspectHandler {
@Around("@annotation(xxxxx.common.annotation.ServiceAutoException)")
public Object serviceExcepHandler(ProceedingJoinPoint proceedingJoinPoint) {
ServiceBaseResult returnMsg;
try {
returnMsg= (ServiceBaseResult) proceedingJoinPoint.proceed();
} catch (Throwable throwable) {
log.error("ServiceExcepHandler serviceExcepHandler failed", throwable);
Signature signature= proceedingJoinPoint.getSignature();
MethodSignature methodSignature = (MethodSignature) signature;
Method method = methodSignature.getMethod();
returnMsg=ServiceBaseResult.LchlT1DefinFromServiceBaseResult(throwable.getMessage(),"");
if (method != null)
{
ServiceAutoException annoObj= method.getAnnotation(ServiceAutoException.class);
if(annoObj!=null){
returnMsg = new ServiceBaseResult(annoObj.str(),
annoObj.level(), annoObj.msg(), annoObj.from());
}
if( throwable instanceof ApiException){
String msg = throwable.getMessage();
returnMsg.setMessage(msg);
}
return returnMsg;
}
}
return returnMsg;
}
ServiceBaseResult 为异常返回类
@Override
@ServiceAutoException(msg = "", from = xxxxx)
public ServiceBaseResult queryMedicalAdviceInfo(MedicalAdviceInfoQueryVO medicalAdviceInfoQueryVO) {
}
在发生异常的时候,切回会处理异常,捕获根据自定义的注解找到对应的切面点,处理