码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2017-10-20 16:07:11      阅读:415      评论:0      收藏:0      [点我收藏+]

标签:ldd   art   返回结果   validate   hand   pen   cts   ring   logs   

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>

 

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

标签:ldd   art   返回结果   validate   hand   pen   cts   ring   logs   

原文地址:http://www.cnblogs.com/xlh91118/p/7699657.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!