dubbo-provider-Validator数据校验

import com.****.logistics.***.contract.CommonResponse;
import com.****.logistics.***.print.pdf.utils.SpringHelper;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.common.extension.Activate;
import org.apache.dubbo.rpc.*;

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.executable.ExecutableValidator;
import java.lang.reflect.Method;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;

/**
 * @author lw
 * @date 2021年06月17日 16:05
 */
@Slf4j
@Activate(
        group = {"provider"}
)
public class ValidatorExceptionFilter implements Filter {

    private static final ExecutableValidator EXECUTABLE_VALIDATOR = Validation.buildDefaultValidatorFactory().getValidator().forExecutables();


    @Override
    public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
        Method                           method               = getMethod(invoker, invocation);
        Class<?>                         inf                  = invoker.getInterface();
        Object                           object               = SpringHelper.getBean(inf);
        Object[]                         paramList            = invocation.getArguments();
        Validator                        validator            = SpringHelper.getBean(Validator.class);
        Set<ConstraintViolation<Object>> constraintViolations = new HashSet<>();
        for (Object o : paramList) {
            constraintViolations.addAll(validator.validate(o));
        }
        CommonResponse commonResponse = getValidationResult(constraintViolations);
        if (Objects.nonNull(commonResponse)) {
            Result result = new AsyncRpcResult(invocation);
            result.setValue(commonResponse);
            return result;
        }
        return invoker.invoke(invocation);
    }

    /**
     * 获取校验方法
     */
    private static Method getMethod(Invoker<?> invoker, Invocation invocation) {
        Method[] methods = invoker.getInterface().getDeclaredMethods();
        for (Method m : methods) {
            boolean needCheck = m.getName().equals(invocation.getMethodName())
                    && invocation.getArguments().length == m.getParameterCount();
            if (needCheck) {
                if (matchMethod(invocation.getParameterTypes(), m.getParameterTypes())) {
                    return m;
                }
            }
        }
        return null;
    }

    /***
     * 获取匹配的方法
     * @param invokerMethodParamClassList
     * @param matchMethodParamClassList
     * @return
     */
    private static boolean matchMethod(Class[] invokerMethodParamClassList, Class[] matchMethodParamClassList) {
        for (int i = 0; i < invokerMethodParamClassList.length; i++) {
            if (!invokerMethodParamClassList[i].equals(matchMethodParamClassList[i])) {
                return false;
            }
        }
        return true;
    }

    /**
     * 校验结果转换返回对象
     */

    private static <T> CommonResponse getValidationResult(Set<ConstraintViolation<T>> set) {
        if (set != null && !set.isEmpty()) {
            StringBuilder stringBuilder = new StringBuilder();
            for (ConstraintViolation<T> violation : set) {
                stringBuilder.append(String.format("%s-%s", violation.getPropertyPath().toString(), violation.getMessage()));
                System.out.println(String.format("-----%s-%s", violation.getPropertyPath().toString(), violation.getMessage()));
            }

            return CommonResponse.fail(String.join(",", stringBuilder.toString()));
        }
        return null;
    }
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.util.Objects;

/**
 * @author lw
 * @date 2021年06月18日 9:29
 */
@Component
public class SpringHelper implements ApplicationContextAware {

    private static ApplicationContext applicationContext;

    @Override
    public synchronized void setApplicationContext(ApplicationContext applicationContext) {
        if (Objects.isNull(SpringHelper.applicationContext)) {
            SpringHelper.applicationContext = applicationContext;
        }
    }

    public static <T> T getBean(Class<T> tClass) {
        return applicationContext.getBean(tClass);
    }

    @SuppressWarnings("unchecked")
    public static <T> T getBean(String beanName) {
        return (T) applicationContext.getBean(beanName);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值