美文网首页
Validate验证工具

Validate验证工具

作者: wang_cheng | 来源:发表于2020-12-02 21:05 被阅读0次

    针对一些业务校验不能放在@Controller层注解校验

    可以支持个性化分组校验

    @Autowired
    private Validator validator;
    
    BeanValidatorsUtils.validateWithException(validator,basicInfoVO,XCAuthBasicInfoVO.authBasic.class);
    
    public class BeanValidatorsUtils {
    
        public BeanValidatorsUtils() {
        }
    
        public static void validateWithException(Validator validator, Object object, Class... groups) throws ConstraintViolationException {
            Set constraintViolations = validator.validate(object, groups);
            if (!constraintViolations.isEmpty()) {
                throw new ConstraintViolationException(constraintViolations);
            }
        }
    
        public static List<String> extractMessage(ConstraintViolationException e) {
            return extractMessage(e.getConstraintViolations());
        }
    
        public static List<String> extractMessage(Set<? extends ConstraintViolation> constraintViolations) {
            List<String> errorMessages = Lists.newArrayList();
            Iterator i$ = constraintViolations.iterator();
    
            while (i$.hasNext()) {
                ConstraintViolation violation = (ConstraintViolation) i$.next();
                errorMessages.add(violation.getMessage());
            }
    
            return errorMessages;
        }
    
        public static Map<String, String> extractPropertyAndMessage(ConstraintViolationException e) {
            return extractPropertyAndMessage(e.getConstraintViolations());
        }
    
        public static Map<String, String> extractPropertyAndMessage(Set<? extends ConstraintViolation> constraintViolations) {
            Map<String, String> errorMessages = Maps.newHashMap();
            Iterator i$ = constraintViolations.iterator();
    
            while (i$.hasNext()) {
                ConstraintViolation violation = (ConstraintViolation) i$.next();
                errorMessages.put(violation.getPropertyPath().toString(), violation.getMessage());
            }
    
            return errorMessages;
        }
    
        public static List<String> extractPropertyAndMessageAsList(ConstraintViolationException e) {
            return extractPropertyAndMessageAsList(e.getConstraintViolations(), " ");
        }
    
        public static List<String> extractPropertyAndMessageAsList(Set<? extends ConstraintViolation> constraintViolations) {
            return extractPropertyAndMessageAsList(constraintViolations, " ");
        }
    
        public static List<String> extractPropertyAndMessageAsList(ConstraintViolationException e, String separator) {
            return extractPropertyAndMessageAsList(e.getConstraintViolations(), separator);
        }
    
        public static List<String> extractPropertyAndMessageAsList(Set<? extends ConstraintViolation> constraintViolations, String separator) {
            List<String> errorMessages = Lists.newArrayList();
            Iterator i$ = constraintViolations.iterator();
    
            while (i$.hasNext()) {
                ConstraintViolation violation = (ConstraintViolation) i$.next();
                errorMessages.add(violation.getPropertyPath() + separator + violation.getMessage());
            }
    
            return errorMessages;
        }
    }
    

    相关文章

      网友评论

          本文标题:Validate验证工具

          本文链接:https://www.haomeiwen.com/subject/pyouwktx.html