zoukankan      html  css  js  c++  java
  • HibernateValidators

    public final class HibernateValidators {

     

        private static final Validator VALIDATOR;

     

        private HibernateValidators() {

        }

     

        static {

            ValidatorFactory factory = Validation.buildDefaultValidatorFactory();

            VALIDATOR = factory.getValidator();

        }

     

     

        public static <T> Set<ConstraintViolation<T>> validate(T object, Class<?>... groups) {

            return VALIDATOR.validate(object, groups);

        }

     

        /**

         * @throws IllegalArgumentException 当校验有错误的时候抛出异常

         */

        public static <T> void throwsIfInvalid(T object, Class<?>... groups) {

            final Set<ConstraintViolation<T>> constraintViolations = validate(object, groups);

            if (!constraintViolations.isEmpty()) {

                final ImmutableMap.Builder<String, String> errorBuilder = ImmutableMap.builder();

                for (ConstraintViolation<T> violation : constraintViolations) {

                    errorBuilder.put(violation.getPropertyPath().toString(), violation.getMessage());

                }

                throw new IllegalArgumentException(errorBuilder.build().toString());

            }

        }

    }

  • 相关阅读:
    企业如何推行白盒测试
    Java命名规范
    MobileVLC for iphoneos4.3
    用Android NDK编译FFmpeg
    Linux 下Android 开发环境搭建 ---CentOS
    为什么要做白盒测试
    vlc的第三方库contrib的修改之一:live库的修改
    VC命名规范
    POJ 1470 Closest Common Ancestors (LCA入门题)
    HDU 4407 Sum 第37届ACM/ICPC 金华赛区 第1008题 (容斥原理)
  • 原文地址:https://www.cnblogs.com/Struts-pring/p/10337066.html
Copyright © 2011-2022 走看看