zoukankan      html  css  js  c++  java
  • 方法复杂对象参数校验器

    import javax.validation.ConstraintViolation;
    
    import org.springframework.stereotype.Service;
    import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
    
    import com.dragon.common.BusinessException;
    import com.dragon.common.enums.BizExceptionEnum;
    
    @Service
    public class MyValidatorBean extends LocalValidatorFactoryBean{
    
    
        public <T> void validates(T object, Class<?>... groups) {
             Set<ConstraintViolation<T>> result =  super.validate(object, groups);
             StringBuilder sb = new StringBuilder();
             for (ConstraintViolation<T> constraintViolation : result) {
                 sb.append(constraintViolation.getPropertyPath()+constraintViolation.getMessage());
             }
             if(sb.length()>1) {
                 throw new BusinessException(BizExceptionEnum.PARAM_ERROR, sb.toString());
             }
        
        }
    }

    支持hibernate.validator下的参数校验注解,

    使用时,注入,直接调用方法即可,这里抛出了我自定义的业务异常然后AOP统一处理

     

  • 相关阅读:
    13.sqoop的安装
    12.Flume的安装
    11.把文本文件的数据导入到Hive表中
    10.hive安装
    9.centos7 安装mysql
    8.时间同步
    7.编写mapreduce案例
    mysql中如何处理字符
    装箱拆箱隐含的问题
    何谓幂等性
  • 原文地址:https://www.cnblogs.com/Ebird/p/14873033.html
Copyright © 2011-2022 走看看