package com.xzx.log.util; import cn.hutool.core.annotation.AnnotationUtil; import cn.hutool.core.exceptions.ExceptionUtil; import cn.hutool.core.util.ReflectUtil; import io.swagger.annotations.ApiOperation; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Component; import java.lang.reflect.Method; @Component @Slf4j public class BusinessUtil { /** * 根据异常获取方法注释 若是中文异常则忽略 * @param e * @return */ public String getMethodAnnoByEx(Exception e){ try { Throwable rootCause = ExceptionUtil.getRootCause(e); if(rootCause!=null){ StackTraceElement[] stackTrace = rootCause.getStackTrace(); if(stackTrace!=null&&stackTrace.length>0){ StackTraceElement stackTraceElement = stackTrace[0]; String methodName = stackTraceElement.getMethodName(); String className = stackTraceElement.getClassName(); Method methodByName = ReflectUtil.getMethodByName(Class.forName(className), methodName); ApiOperation annotation = AnnotationUtil.getAnnotation(methodByName, ApiOperation.class); if(annotation!=null){ return annotation.value()+"失败"; } } } }catch (Exception e1) { return null; } return null; } }