通过aop实现rpc统一参数校验&异常捕捉

api rpc 统一参数校验和异常拦截

hibernate validator工具类 
public class ValidatorUtils {
    private ValidatorUtils() {
    }

    private static Validator validator;

    public static <T> void validate(T obj) {
        Set<ConstraintViolation<T>> constraintViolations = getValidator().validate(obj);
        if (CollectionUtils.isEmpty(constraintViolations)) {
            return;
        }
        StringBuffer paramMsg = new StringBuffer();
        constraintViolations.forEach(violation -> paramMsg.append(violation.getMessage()).append(";"));
        throw new ParamInvalidException(paramMsg.toString());
    }

    private static Validator getValidator() {
        if (Objects.isNull(validator)) {
            ValidatorFactory validatorFactory = Validation.buildDefaultValidatorFactory();
            validator = validatorFactory.getValidator();
        }
        return validator;
    }

}

切面

@Aspect
@Component
@Slf4j
public class ServiceAspect {

    @Pointcut("execution(public com.xxx.rpc.share.base.ResponseDTO *(..))")
    public void exeMethod() {
    }

    @Around("exeMethod()")
    public <T> ResponseDTO<T> around(ProceedingJoinPoint pjp) {
        Object[] params = pjp.getArgs();
        try {
            // 入参校验
            this.paramValidate(params);
            Object proceed = pjp.proceed();
            String methodName = pjp.getSignature().getName();
            if (Objects.isNull(proceed)) {
                log.error("服务接口【{}】返回空,请求参数:{}", methodName, Arrays.toString(params));
                throw new NullPointerException("服务返回结果为空");
            }
            return (ResponseDTO<T>) proceed;
        } catch (Throwable t) {
            return handleException(t);
        }
    }

    private void paramValidate(Object[] params) {
        if (ArrayUtils.isEmpty(params)) {
            return;
        }
        Stream.of(params).forEach(param -> {
            if (Objects.isNull(param)) {
                throw new ParamInvalidException("传入参数为空!");
            }
            ValidatorUtils.validate(param);
        });
    }

    private <T> ResponseDTO<T> handleException(Throwable t) {
        ResponseDTO<T> responseDTO = new ResponseDTO<>();
        if (t instanceof ParamInvalidException) {
            responseDTO.setResult(ResponseCode.ILLPARAM, t.getMessage());
        } else {
            log.error("系统异常:{}", t);
            responseDTO.setResult(ResponseCode.EXCEPTION, t.getMessage());
        }
        return responseDTO;
    }

依赖jar

    <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.18</version>
            <!--<scope>provided</scope>-->
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>5.3.5.Final</version>
        </dependency>
        <dependency>
            <groupId>javax.el</groupId>
            <artifactId>el-api</artifactId>
            <version>2.2</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.web</groupId>
            <artifactId>javax.el</artifactId>
            <version>2.2.6</version>
        </dependency>

 

posted @ 2017-10-20 15:16  蝼蚁尚不自知  阅读(2789)  评论(0编辑  收藏  举报