import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; @Data @AllArgsConstructor @NoArgsConstructor public class CommonResponse<T> { private T data; private String message; private int code; public static <T> CommonResponse<T> success(T data) { return new CommonResponse<>(data, "", 0); } public static <T> CommonResponse<T> fail(String message) { return new CommonResponse<>(null, message, -1); } }
public class CheckException extends RuntimeException { public CheckException() { } public CheckException(String message) { super(message); } public CheckException(String message, Throwable cause) { super(message, cause); } public CheckException(Throwable cause) { super(cause); } public CheckException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { super(message, cause, enableSuppression, writableStackTrace); } }
import jakarta.annotation.Resource; import lombok.extern.slf4j.Slf4j; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.springframework.stereotype.Component; @Slf4j @Aspect @Component public class ControllerAop { @Resource PerformanceService performanceService; @Around("execution(public xxxxxxx.CommonResponse *(..))") public Object handlerControllerMethod(ProceedingJoinPoint pjp) { CommonResponse<?> result; try { result = (CommonResponse<?>) pjp.proceed(); } catch (Throwable e) { result = handleException(pjp, e); } return result; } private CommonResponse<?> handleException(ProceedingJoinPoint pjp, Throwable e) { CommonResponse<?> result; if (e instanceof CheckException) { result = CommonResponse.fail(e.getMessage()); } else { log.error("{} error happened ", pjp.getSignature(), e); result = CommonResponse.fail("发生未知异常"); } return result; } }
标签:返回,Springboot,接口,CheckException,result,CommonResponse,import,message,public From: https://www.cnblogs.com/namedlxd/p/18408157