/**
|
* projectName: h3-organization
|
* fileName: ParameterValidate.java
|
* packageName: com.hydee.common.validate
|
* date: 2019-06-17 10:48
|
* copyright(c) 2019 http://www.hydee.cn/ Inc. All rights reserved.
|
*/
|
package com.matrix.system.common.validate;
|
|
import com.matrix.system.common.validate.beans.ErrorMessage;
|
import com.matrix.system.common.validate.group.Group;
|
import org.hibernate.validator.HibernateValidator;
|
|
import javax.validation.ConstraintViolation;
|
import javax.validation.Validation;
|
import javax.validation.Validator;
|
import java.util.ArrayList;
|
import java.util.List;
|
import java.util.Set;
|
|
/**
|
* @version: V1.0
|
* @author: LiHengye
|
* @className: ParameterValidate
|
* @packageName: com.hydee.common.validate
|
* @description: 参数验证
|
* @data: 2019-06-17 10:48
|
**/
|
public class ParameterValidate {
|
|
|
/**
|
* 开启快速结束模式 failFast (true)
|
*/
|
private static Validator validator = Validation.byProvider(HibernateValidator.class).configure().failFast(true).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.getMessageTemplate());
|
}
|
}
|
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, Group.ADD.class);
|
boolean hasError = violationSet != null && violationSet.size() > 0;
|
result.setHasErrors(hasError);
|
if (hasError) {
|
for (ConstraintViolation<T> violation : violationSet) {
|
result.addError(propertyName, violation.getMessage());
|
}
|
}
|
return result;
|
}
|
|
/**
|
* 校验结果类
|
*/
|
public static class ValidResult {
|
|
/**
|
* 是否有错误
|
*/
|
private boolean hasErrors;
|
|
/**
|
* 错误信息
|
*/
|
private List<ErrorMessage> errors;
|
|
public ValidResult() {
|
this.errors = new ArrayList<>();
|
}
|
public boolean hasErrors() {
|
return hasErrors;
|
}
|
|
public void setHasErrors(boolean hasErrors) {
|
this.hasErrors = hasErrors;
|
}
|
|
/**
|
* 获取所有验证信息
|
* @return 集合形式
|
*/
|
public List<ErrorMessage> getAllErrors() {
|
return errors;
|
}
|
/**
|
* 获取所有验证信息
|
* @return 字符串形式
|
*/
|
public String getErrors(){
|
StringBuilder sb = new StringBuilder();
|
for (ErrorMessage error : errors) {
|
sb.append(error.getPropertyPath()).append(":不能为空");
|
// sb.append(error.getPropertyPath()).append(":").append(error.getMessage()).append(" ");
|
}
|
return sb.toString();
|
}
|
|
public String getAllErrorMessage(){
|
StringBuilder sb = new StringBuilder();
|
for (ErrorMessage error : errors) {
|
if(error.getMessage() == null || error.getMessage().isEmpty()){
|
sb.append(error.getPropertyPath()).append(":不合法!");
|
}else{
|
sb.append(error.getPropertyPath()).append(":").append(error.getMessage()).append(" ");
|
}
|
}
|
return sb.toString();
|
}
|
|
public void addError(String propertyPath, String message) {
|
this.errors.add(new ErrorMessage(propertyPath, message));
|
}
|
}
|
}
|