All Downloads are FREE. Search and download functionalities are using the official Maven repository.

vite.utils.ValidationUtils Maven / Gradle / Ivy

There is a newer version: 0.2.1
Show newest version
package vite.utils;

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;
import java.util.Set;

/**
 * 字段校验工具类
 */
public class ValidationUtils {

    public static Validator getValidator() {
        return validator;
    }

    static Validator validator;

    static {
        ValidatorFactory validatorFactory = Validation.buildDefaultValidatorFactory();
        validator = validatorFactory.getValidator();
    }

    public static void validator(Object object) {
        Set> violations = ValidationUtils.getValidator().validate(object);

        if (!violations.isEmpty()) {
            StringBuffer sb = new StringBuffer();
            for (ConstraintViolation constraintViolation : violations) {
                sb.append(constraintViolation.getPropertyPath().toString());
                sb.append(":");
                sb.append(constraintViolation.getMessage());
                sb.append(";");
            }
            throw new RuntimeException(sb.toString());
        }

    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy