导入统一校验
This commit is contained in:
+142
@@ -0,0 +1,142 @@
|
||||
package com.jero.common.util;
|
||||
|
||||
import com.jero.common.exception.JeroBootException;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import javax.validation.ConstraintViolation;
|
||||
import javax.validation.Validation;
|
||||
import javax.validation.Validator;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* 手动校验@Vaild方法
|
||||
* @author liJiaRao
|
||||
* @date 2021-08-05 15:09
|
||||
*/
|
||||
@Slf4j
|
||||
public class ValidUtil {
|
||||
|
||||
private static Validator validator;
|
||||
|
||||
static {
|
||||
validator = Validation.buildDefaultValidatorFactory()
|
||||
.getValidator();
|
||||
}
|
||||
|
||||
private static Validator validatorAll;
|
||||
|
||||
static {
|
||||
validatorAll = Validation.buildDefaultValidatorFactory()
|
||||
.getValidator();
|
||||
}
|
||||
|
||||
/**
|
||||
* 手动校验@Vaild并抛出异常信息
|
||||
*
|
||||
* @param object 待校验对象
|
||||
* @param groups 待校验的组
|
||||
*/
|
||||
public static void validate(Object object, Class<?>... groups){
|
||||
StringBuilder msg = new StringBuilder();
|
||||
Set<ConstraintViolation<Object>> set = validator.validate(object, groups);
|
||||
for (ConstraintViolation<Object> constraintViolation : set) {
|
||||
msg.append(constraintViolation.getMessage()).append(",");
|
||||
}
|
||||
if (StringUtils.isNotBlank(msg)) {
|
||||
throw new JeroBootException(msg.substring(0,msg.length()-1));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 手动校验@Vaild并抛出异常信息list
|
||||
*
|
||||
* @param object 待校验对象
|
||||
* @param groups 待校验的组
|
||||
* @return errorList
|
||||
*/
|
||||
public static List<String> validateReturnList(Object object, Class<?>... groups){
|
||||
List<String> list = new ArrayList<>();
|
||||
Set<ConstraintViolation<Object>> set = validator.validate(object, groups);
|
||||
for (ConstraintViolation<Object> constraintViolation : set) {
|
||||
list.add(constraintViolation.getMessage());
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
/**
|
||||
* 手动校验@Vaild并抛出异常信息list
|
||||
*
|
||||
* @param object 待校验对象
|
||||
* @param groups 待校验的组
|
||||
* @return errorList
|
||||
*/
|
||||
public static List<String> validateReturnListWithNum(Object object, int errorRowNum, Class<?>... groups){
|
||||
List<String> list = new ArrayList<>();
|
||||
Set<ConstraintViolation<Object>> set = validator.validate(object, groups);
|
||||
for (ConstraintViolation<Object> constraintViolation : set) {
|
||||
list.add("第" + errorRowNum + "行:"+constraintViolation.getMessage());
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
/**
|
||||
* @Description 校验全部数据格式
|
||||
* @Author lqt
|
||||
* @Date 2021/8/26
|
||||
* @Param
|
||||
* @Return
|
||||
* @Exception
|
||||
*/
|
||||
public static StringBuilder validateAll(Object object){
|
||||
StringBuilder msg = new StringBuilder();
|
||||
Set<ConstraintViolation<Object>> set = validatorAll.validate(object);
|
||||
if(set != null && set.size() >0 ){
|
||||
for(ConstraintViolation<Object> constraintViolation : set){
|
||||
// 这里循环获取错误信息,可以自定义格式
|
||||
msg.append(constraintViolation.getMessage()).append("<br/>");
|
||||
}
|
||||
}
|
||||
return msg;
|
||||
}
|
||||
/**
|
||||
* @Description 校验excel 每行数据格式
|
||||
* @Author lqt
|
||||
* @Date 2021/8/26
|
||||
* @Param count excel 行数
|
||||
* @Return
|
||||
* @Exception
|
||||
*/
|
||||
public static StringBuilder validateExcel(Object object, int count){
|
||||
StringBuilder msg = new StringBuilder();
|
||||
Set<ConstraintViolation<Object>> set = validatorAll.validate(object);
|
||||
if(set != null && set.size() > 0 ){
|
||||
for(ConstraintViolation<Object> constraintViolation : set){
|
||||
// 这里循环获取错误信息,可以自定义格式
|
||||
msg.append("第").append(count).append("行:").append(constraintViolation.getMessage()).append(",请修改。<br/>");
|
||||
}
|
||||
}
|
||||
return msg;
|
||||
}
|
||||
/**
|
||||
* @Description 校验excel 每行数据格式(只用于支出合同导入校验)
|
||||
* @Author lqt
|
||||
* @Date 2021/8/26
|
||||
* @Param count excel 行数
|
||||
* @Return
|
||||
* @Exception
|
||||
*/
|
||||
public static StringBuilder validateSpendingContractExcel(Object object, int count,int num){
|
||||
StringBuilder msg = new StringBuilder();
|
||||
Set<ConstraintViolation<Object>> set = validatorAll.validate(object);
|
||||
if(set != null && set.size() > 0 ){
|
||||
for(ConstraintViolation<Object> constraintViolation : set){
|
||||
// 这里循环获取错误信息,可以自定义格式
|
||||
msg.append("第").append(count).append("行:").append(num).append(constraintViolation.getMessage()).append(",请修改。<br/>");
|
||||
}
|
||||
}
|
||||
return msg;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user