1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
|
import javax.validation.*;
import java.util.Set;
public class VlidationUtil {
private static Validator validator;
static { ValidatorFactory vf = Validation.buildDefaultValidatorFactory(); validator = vf.getValidator(); }
public static <T> String validate(T t) throws ValidationException{ Set<ConstraintViolation<T>> set = validator.validate(t); StringBuilder validateError = null; if(set.size()>0){ validateError = new StringBuilder(); for(ConstraintViolation<T> val : set){ validateError.append(val.getInvalidValue()+ "," + val.getMessage() + " ;"); } return validateError.toString(); }
return null;
} }
|