package com.ruoyi.system.service.imports;
import com.alibaba.excel.annotation.ExcelProperty;
import com.ruoyi.system.service.imports.valid.EnumValidator;
import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.groups.Default;
import java.lang.reflect.Field;
import java.util.Set;
/**
* @date :Created 2022/12/28 13:00
* @description :校验工具
*/
public class EasyExcelValidHelper {
private EasyExcelValidHelper() {
}
/**
* 校验常量
*/
private static final Validator VALIDATOR = Validation.buildDefaultValidatorFactory().getValidator();
/**
* description 对象校验
*
* @param entity 导入数据
* @return java.lang.String
**/
public static <T> String validateEntity(T entity) throws NoSuchFieldException {
StringBuilder result = new StringBuilder();
Set<ConstraintViolation<T>> set = VALIDATOR.validate(entity, Default.class);
if (set != null && !set.isEmpty()) {
for (ConstraintViolation<T> cv : set) {
Field declaredField = entity.getClass().getDeclaredField(cv.getPropertyPath().toString());
ExcelProperty annotation = declaredField.getAnnotation(ExcelProperty.class);
result.append(annotation.value()[0]).append(cv.getMessage()).append("; ");
}
}
return result.toString();
}
}
枚举校验
package com.ruoyi.system.service.imports.valid;
import javax.validation.Constraint;
import javax.validation.Payload;
import java.lang.annotation.*;
/**
* @date :Created 2022/12/28 13:00
* @description :校验枚举注解
*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.METHOD})
@Constraint(validatedBy = EnumValidator.class)
public @interface EnumValid {
/**
* 枚举的类型
*/
Class<?> value();
/**
* 错误消息
*
* @return
*/
String message() default "枚举类型的值不正确";
/**
* 获取枚举值的方法
*/
String method() default "getCode";
String method2() default "";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}
package com.ruoyi.system.service.imports.valid;
import com.ruoyi.common.core.utils.bean.BeanUtils;
import org.apache.commons.compress.utils.Lists;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;
/**
* @date :Created 2022/12/28 14:40
* @description :枚举验证
*/
public class EnumValidator implements ConstraintValidator<EnumValid, Object> {
private static final Logger logger = LoggerFactory.getLogger(EnumValidator.class);
/**
* 存具体枚举的值
*/
private final List<Object> values = Lists.newArrayList();
@Override
public void initialize(EnumValid constraintAnnotation) {
Class<?> enumClazz = constraintAnnotation.value();
Object[] enumConstants = enumClazz.getEnumConstants();
if (null == enumConstants) {
return;
}
Method method = BeanUtils.findMethod(enumClazz, constraintAnnotation.method());
if (null == method) {
logger.warn("枚举对象:[{}]中不存在方法:[{}],请检查.", enumClazz.getName(), constraintAnnotation.method());
throw new RuntimeException("枚举对象中不存在获取值的方法");
}
method.setAccessible(true);
try {
for (Object enumConstant : enumConstants) {
values.add(method.invoke(enumConstant));
}
} catch (IllegalAccessException | InvocationTargetException e) {
logger.warn("获取枚举类:[{}]的枚举对象的值失败.", enumClazz);
throw new RuntimeException("获取枚举值失败");
}
Method method2 = BeanUtils.findMethod(enumClazz, constraintAnnotation.method2());
if (null != method2) {
// method2.setAccessible(true);
try {
for (Object enumConstant : enumConstants) {
values.add(method2.invoke(enumConstant));
}
} catch (IllegalAccessException | InvocationTargetException e) {
logger.warn("获取枚举类:[{}]的枚举对象的值失败.", enumClazz);
throw new RuntimeException("获取枚举值失败");
}
}
}
@Override
public boolean isValid(Object value, ConstraintValidatorContext context) {
return null == value || values.contains(value);
}
}
package com.ruoyi.system.domain.imports;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.write.style.ColumnWidth;
import com.ruoyi.system.domain.enums.AppLoginTypeEnum;
import com.ruoyi.system.domain.export.BaseExportData;
import com.ruoyi.system.service.imports.valid.EnumValid;
import org.hibernate.validator.constraints.Length;
import javax.validation.constraints.NotBlank;
/**
* 应用导入数据类
**/
public class AppCfgExcelData extends BaseExportData {
@Length(max=20)
@NotBlank(message = "不可空")
@ColumnWidth(20)
@ExcelProperty("应用ID")
private String appId;
@NotBlank(message = "不可空")
@ColumnWidth(20)
@ExcelProperty("应用名称")
private String appName;
@NotBlank(message = "不可空")
@ColumnWidth(20)
@ExcelProperty("应用类型")
private String appTypeName;
@NotBlank(message = "不可空")
@ColumnWidth(20)
@ExcelProperty("应用子类型")
private String appSubtypeName;
@EnumValid(value = AppLoginTypeEnum.class, method2 = "getName", message = "值不正确")
@NotBlank(message = "不可空")
@ColumnWidth(10)
@ExcelProperty("登录类型")
private String loginTypeName;
@NotBlank(message = "不可空")
@ColumnWidth(10)
@ExcelProperty("发布状态")
private String releaseStateName;
@NotBlank(message = "不可空")
@ColumnWidth(10)
@ExcelProperty("显示顺序")
private String orderNum;
/**
* 错误信息
*/
@ExcelProperty("错误描述")
private String error;
public String getAppId() {
return appId;
}
public void setAppId(String appId) {
this.appId = appId;
}
public String getAppName() {
return appName;
}
public void setAppName(String appName) {
this.appName = appName;
}
public String getAppTypeName() {
return appTypeName;
}
public void setAppTypeName(String appTypeName) {
this.appTypeName = appTypeName;
}
public String getAppSubtypeName() {
return appSubtypeName;
}
public void setAppSubtypeName(String appSubtypeName) {
this.appSubtypeName = appSubtypeName;
}
public String getLoginTypeName() {
return loginTypeName;
}
public void setLoginTypeName(String loginTypeName) {
this.loginTypeName = loginTypeName;
}
public String getReleaseStateName() {
return releaseStateName;
}
public void setReleaseStateName(String releaseStateName) {
this.releaseStateName = releaseStateName;
}
public String getOrderNum() {
return orderNum;
}
public void setOrderNum(String orderNum) {
this.orderNum = orderNum;
}
public String getError() {
return error;
}
public void setError(String error) {
this.error = error;
}
}
标签:return,String,easyexcel,校验,枚举,导入,import,com,public
From: https://www.cnblogs.com/stubborn-dude/p/17014229.html