必填参数统一处理

2019-09-10 18:12:12 浏览数 (1)

1. 必填参数统一处理

1.1. 需求

  1. 项目使用交易代码的形式,反射调用执行类,统一入口和出口类,相当于直接调用服务层,例如:
代码语言:javascript复制
 /**
  * 获取订单详情3003101009
  */
 ResponseReport getOrderDetail(RequestReport requestReport);
  1. 我们知道如果后台调用入口在Controller,且入口参数灵活填写,那么可以使用SpringMVC自带的参数验证方式,通过注解如@NotNull @Size等来限制入参,错误直接返回
  2. 现在我调用入口参数统一了,也不在controller层,但要实现相同的功能,也就是参数验证,特别是必填项

1.2. 解决

1.2.1. 服务层

  1. 我的想法同样是通过注解来解决,只是注解的实现方法需要自己实现
  2. 通过aop切服务层固定入参列表的方法,判断是否存在注解
代码语言:javascript复制
@Override
@CheckUnfilled(value={"order_id"})
public ResponseReport getOrderDetail(RequestReport requestReport) {
    ResponseReport responseReport = new ResponseReport();
    Map<String, Object> reqParams = JSON.parseObject(requestReport.getBody().toString(), Map.class);
    Object id = reqParams.get("order_id");
    String orderId = id.toString();
    OrderInfo orderInfo = orderInfoExtDao.selectByPrimaryKey(orderId);
    OrderInfoExt orderInfoExt = new OrderInfoExt();
    orderInfoExt.setAddress(orderInfo.getHospital()   " "   orderInfo.getBed());
    BeanUtils.copyProperties(orderInfo, orderInfoExt);
    return responseReport.returnSuccessResult(orderInfoExt, "获取订单详情成功", requestReport);
}

1.2.2. 注解方法

代码语言:javascript复制
/**
 * @author laoliangliang
 * @date 2019/1/10 16:57
 */
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface CheckUnfilled {
    String[] value() default {};
}

1.2.3. 注解实现aop

代码语言:javascript复制
package com.zhiyis.framework.aop;

import com.alibaba.fastjson.JSON;
import com.zhiyis.common.report.RequestReport;
import com.zhiyis.common.report.ResponseReport;
import com.zhiyis.framework.annotation.CheckUnfilled;
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.core.annotation.AnnotationUtils;
import org.springframework.stereotype.Component;
import org.springframework.util.ReflectionUtils;

import java.lang.reflect.Method;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * @author laoliangliang
 * @date 2019/1/10 17:04
 */
@Slf4j
@Component
@Aspect
public class CheckUnfilledAspect {

    @Around("execution(com.zhiyis.common.report.ResponseReport com.zhiyis.framework.service.impl..*.*(com.zhiyis.common.report.RequestReport))")
    public Object checkUnfilled(ProceedingJoinPoint joinPoint) {
        Object clazz = joinPoint.getTarget();
        String methodName = joinPoint.getSignature().getName();
        Object[] args = joinPoint.getArgs();
        RequestReport requestReport = (RequestReport) args[0];
        Map<String, Object> reqParams = JSON.parseObject(JSON.toJSONString(requestReport.getBody()), Map.class);

        Method method = ReflectionUtils.findMethod(clazz.getClass(), methodName, RequestReport.class);

        CheckUnfilled annotation = AnnotationUtils.findAnnotation(method, CheckUnfilled.class);
        if(annotation != null){
            String[] value = annotation.value();
            Set<String> keys = reqParams.keySet();
            Set<String> unfilledSet = new HashSet<>();
            for (String key : value) {
                if (!keys.contains(key)) {
                    unfilledSet.add(key);
                }
            }
            if (unfilledSet.size() > 0) {
                ResponseReport responseReport = new ResponseReport();
                return responseReport.returnError("9999", JSON.toJSONString(unfilledSet)   "为必填参数");
            }
        }
        try {
            return joinPoint.proceed();
        } catch (Throwable e) {
            log.error("tradeCode接口错误", e);
        }
        return null;
    }


}

1.3. 测试

  1. 带orderId参数
  1. 不带orderId参数
  1. 上述接口只为测试所写,这种没有任何验证的在正式环境存在开关关闭执行
aop

0 人点赞