初始代码
This commit is contained in:
@@ -0,0 +1,23 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<artifactId>yunzhupaas-boot-common</artifactId>
|
||||
<groupId>com.yunzhupaas</groupId>
|
||||
<version>5.2.0-RELEASE</version>
|
||||
<relativePath>../../pom.xml</relativePath>
|
||||
</parent>
|
||||
|
||||
<artifactId>yunzhupaas-common-office-v3</artifactId>
|
||||
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.yunzhupaas</groupId>
|
||||
<artifactId>yunzhupaas-common-office</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,70 @@
|
||||
package cn.afterturn.easypoi.util;
|
||||
|
||||
import cn.afterturn.easypoi.excel.annotation.Excel;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import jakarta.validation.ConstraintViolation;
|
||||
import jakarta.validation.Validation;
|
||||
import jakarta.validation.Validator;
|
||||
import jakarta.validation.ValidatorFactory;
|
||||
|
||||
/**
|
||||
* 导入可选校验 ImportParams.needVerify
|
||||
* HIBERNATE 校验工具类
|
||||
*
|
||||
* @author JueYue
|
||||
* 2015年11月11日 下午10:04:07
|
||||
*/
|
||||
public class PoiValidationUtil {
|
||||
|
||||
private final static Validator VALIDATOR;
|
||||
|
||||
static {
|
||||
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
|
||||
VALIDATOR = factory.getValidator();
|
||||
}
|
||||
|
||||
public static String validation(Object obj, Class[] verfiyGroup) {
|
||||
Set<ConstraintViolation<Object>> set = null;
|
||||
if (verfiyGroup != null) {
|
||||
set = VALIDATOR.validate(obj, verfiyGroup);
|
||||
} else {
|
||||
set = VALIDATOR.validate(obj);
|
||||
}
|
||||
if (set != null && set.size() > 0) {
|
||||
return getValidateErrMsg(set);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private static String getValidateErrMsg(Set<ConstraintViolation<Object>> set) {
|
||||
StringBuilder builder = new StringBuilder();
|
||||
for (ConstraintViolation<Object> constraintViolation : set) {
|
||||
Class<?> cls = constraintViolation.getRootBean().getClass();
|
||||
String fieldName = constraintViolation.getPropertyPath().toString();
|
||||
List<Field> fields = new ArrayList<>(Arrays.asList(cls.getDeclaredFields()));
|
||||
Class<?> superClass = cls.getSuperclass();
|
||||
if (superClass != null) {
|
||||
fields.addAll(Arrays.asList(superClass.getDeclaredFields()));
|
||||
}
|
||||
String name = null;
|
||||
for (Field field : fields) {
|
||||
if (field.getName().equals(fieldName) && field.isAnnotationPresent(Excel.class)) {
|
||||
name = field.getAnnotation(Excel.class).name();
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (name == null) {
|
||||
name = fieldName;
|
||||
}
|
||||
builder.append(name).append(constraintViolation.getMessage()).append(",");
|
||||
}
|
||||
return builder.substring(0, builder.length() - 1);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user