Aop全局异常优雅处理

自定义异常ClientException

package com.example.demo.base;

/**
 * @author: robin
 * @date: 2020/7/15 7:52 PM
 * @aphorism: "stay hungry, stay foolish"
 */
public class ClientException extends RuntimeException{
    private int errorCode;

    public ClientException(int errorCode) {
        this.errorCode = errorCode;
    }

    public ClientException(String message, int errorCode) {
        super(message);
        this.errorCode = errorCode;
    }

    public ClientException(String message, Throwable cause, int errorCode) {
        super(message, cause);
        this.errorCode = errorCode;
    }

    public ClientException(Throwable cause, int errorCode) {
        super(cause);
        this.errorCode = errorCode;
    }

    public ClientException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace, int errorCode) {
        super(message, cause, enableSuppression, writableStackTrace);
        this.errorCode = errorCode;
    }
}

自定义Annotation

package com.example.demo.base;

import java.lang.annotation.*;

/**
 * @author: robin
 * @date: 2020/7/15 7:55 PM
 * @aphorism: "stay hungry, stay foolish"
 */
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface ClientExceptionAnnotation {
    int errorCode() default 0;
    String errorMsg() default "ok";
}

Aspect:

package com.example.demo.base.aspect;

import com.example.demo.base.ClientException;
import com.example.demo.model.Result;
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: robin
 * @date: 2020/7/15 7:57 PM
 * @aphorism: "stay hungry, stay foolish"
 */
@Component
@Aspect
@Slf4j
public class ClientExceptionAspect {
    @Pointcut(value = "@annotation(com.example.demo.base.ClientExceptionAnnotation)")
    public void checkException() {
    }

    @Around("checkException()")
    public Object ExceptionAspect(ProceedingJoinPoint joinPoint) {
        long startTime = System.currentTimeMillis();
        MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
        //className+methodName
        String methodName = joinPoint.getTarget().getClass().getName() + "." + methodSignature.getName();
        // request
        Object[] args = joinPoint.getArgs();

        Result ret = null;

        try {
            ret = (Result) joinPoint.proceed();
        } catch (Throwable e) {
            if (e instanceof ClientException) {
                //todo
            } else {
                //todo

            }
        }
        long endTime = System.currentTimeMillis();
        log.info("call method{} ({}) 耗时:{}秒", methodName, args, (endTime - startTime) / 1000);
        return ret;

    }
}

具体使用:

package com.example.demo.controller;

import com.example.demo.base.ClientException;
import com.example.demo.base.ClientExceptionAnnotation;
import com.example.demo.model.Result;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author: robin
 * @date: 2020/7/15 8:14 PM
 * @aphorism: "stay hungry, stay foolish"
 */
@RestController
public class QueryStudentInfoCon {

    @GetMapping(value = "/student")
    @ClientExceptionAnnotation
    public Result queryInfo() {
        int i = 1;
        if (i + 1 == 2) {
            throw new ClientException("A0001", null, 123);
        }
        return new Result(200, "", null);
    }
}

你可能感兴趣的:(Aop全局异常优雅处理)