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统一处理

方法复杂对象参数校验器

 

 方法复杂对象参数校验器

 

相关文章:

  • 2021-07-19
  • 2022-12-23
  • 2021-08-02
  • 2021-09-27
  • 2022-12-23
  • 2022-12-23
  • 2021-05-11
猜你喜欢
  • 2022-01-05
  • 2021-10-25
  • 2021-07-17
  • 2021-11-20
  • 2022-12-23
  • 2022-12-23
  • 2021-09-08
相关资源
相似解决方案