一:工具类集成
public class ValidatorUtils {
private static Validator validator;
static {
validator = Validation.buildDefaultValidatorFactory().getValidator();
}
public static String returnValidateEntity(Object object, Class>... groups) throws ViedNotException {
Set> constraintViolations = validator.validate(object, groups);
StringBuilder msg = new StringBuilder();
if (!constraintViolations.isEmpty()) {
return msg.append(constraintViolations.iterator().next().getMessage()).append(";").toString();
}
return msg.toString();
}
public static void validateEntity(Object object, Class>... groups) throws ViedNotException {
Set> constraintViolations = validator.validate(object, groups);
StringBuilder msg = new StringBuilder();
if (!constraintViolations.isEmpty()) {
throw new ViedNotException (msg.append(constraintViolations.iterator().next().getMessage()).append(";").toString());
}
}
}
二:调用方式



