vite.utils.ValidationUtils Maven / Gradle / Ivy
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());
}
}
}