- 工具类
import org.hibernate.validator.HibernateValidator;
import javax.validation.ConstraintViolation;
import javax.validation.Validator;
import javax.validation.Validation;
import java.util.Set;
public class ValidatorUtil {
/**
* 开启快速结束模式 failFast (true)
*/
private static final Validator validator = Validation.byProvider(HibernateValidator.class).configure().failFast(false).buildValidatorFactory().getValidator();
/**
* 校验对象
*
* @param t bean
* @param groups 校验组
* @return ValidResult
*/
public static <T> ValidResult validateBean(T t, Class<?>...groups) {
ValidResult result = new ValidResult();
Set<ConstraintViolation<T>> violationSet = validator.validate(t,groups);
boolean hasError = violationSet != null && violationSet.size() > 0;
result.setHasErrors(hasError);
if (hasError) {
for (ConstraintViolation<T> violation : violationSet) {
result.addError(violation.getPropertyPath().toString(), violation.getMessage());
}
}
return result;
}
/**
* 校验bean的某一个属性
*
* @param obj bean
* @param propertyName 属性名称
* @return ValidResult
*/
public static <T> ValidResult validateProperty(T obj, String propertyName) {
ValidResult result = new ValidResult();
Set<ConstraintViolation<T>> violationSet = validator.validateProperty(obj, propertyName);
boolean hasError = violationSet != null && violationSet.size() > 0;
result.setHasErrors(hasError);
if (hasError) {
for (ConstraintViolation<T> violation : violationSet) {
result.addError(propertyName, violation.getMessage());
}
}
return result;
}
}
2. ValidResult 校验结果类
import java.util.ArrayList;
import java.util.List;
public class ValidResult {
/**
* 是否有错误
*/
private boolean hasErrors;
/**
* 错误信息
*/
private final List<ValidErrorMessage> errors;
public ValidResult() {
this.errors = new ArrayList<>();
}
public boolean hasErrors() {
return hasErrors;
}
public void setHasErrors(boolean hasErrors) {
this.hasErrors = hasErrors;
}
/**
* 获取所有验证信息
* @return 集合形式
*/
public List<ValidErrorMessage> getAllErrors() {
return errors;
}
/**
* 获取所有验证信息
* @return 字符串形式
*/
public String getErrors(){
StringBuilder sb = new StringBuilder();
for (ValidErrorMessage error : errors) {
sb.append(error.getPropertyPath()).append(":").append(error.getMessage()).append(" ");
}
return sb.toString();
}
public void addError(String propertyName, String message) {
this.errors.add(new ValidErrorMessage(propertyName, message));
}
}
3. 错误类
public class ValidErrorMessage {
private String propertyPath;
private String message;
public String getPropertyPath() {
return propertyPath;
}
public void setPropertyPath(String propertyPath) {
this.propertyPath = propertyPath;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public ValidErrorMessage() {
}
public ValidErrorMessage(String propertyPath, String message) {
this.propertyPath = propertyPath;
this.message = message;
}
}
4. 校验类
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotEmpty;
public class CheckDataUpdateTimeJson {
@NotBlank(message = "token不能为空!")
private String token;
public void setToken(String token) {
this.token = token;
}
public String getToken() {
return token;
}
}
5. 使用
String requestJson = HelperUtils.convertStreamToString(request.getInputStream());
CheckDataUpdateTimeJson paramJson = JSON.parseObject(requestJson, CheckDataUpdateTimeJson.class);
ValidResult validResult = ValidatorUtil.validateBean(paramJson);
if (validResult.hasErrors()) {
System.out.println(validResult.getErrors());
}