注解的方式校验
1.声明不能为空校验注解
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* <p>Excel导入必填校验注解</p>
*
*/
@Target({ ElementType.FIELD, ElementType.TYPE })
@Retention(RetentionPolicy.RUNTIME)
public @interface ExcelValid {
String message() default "导入有未填入的字段";
}
2.excel模板对象相应属性添加注解
3.编写字段校验类(类似监听器的作用)
ExceptionCustom为继承RuntimeException 的自定义异常类
import com.mique.annotation.ExcelValid;
import com.mique.utils.exception.ExceptionCustom;
import java.lang.reflect.Field;
import java.util.Objects;
/**
* <p>Excel导入字段校验</p>
*
* created By DoLaLi on 2022/3/4
*/
public class ExcelImportValid {
/**
* Excel导入字段校验
*
* @param object 校验的JavaBean 其属性须有自定义注解
*/
public static void valid(Object object) throws ExceptionCustom{
Field[] fields = object.getClass().getDeclaredFields();
for (Field field : fields) {
//设置可访问
field.setAccessible(true);
//属性的值
Object fieldValue = null;
try {
fieldValue = field.get(object);
} catch (IllegalAccessException e) {
throw new ExceptionCustom("IMPORT_PARAM_CHECK_FAIL", "导入参数检查失败");
}
//是否包含必填校验注解
boolean isExcelValid = field.isAnnotationPresent(ExcelValid.class);
if (isExcelValid && Objects.isNull(fieldValue)) {
throw new ExceptionCustom("NULL", field.getAnnotation(ExcelValid.class).message());
}
}
}
}
4.在继承AnalysisEventListener类的public void invoke(PatientExcelModel data, AnalysisContext analysisContext)方法中加校验
@Override
public void invoke(PatientExcelModel patientExcelModel, AnalysisContext analysisContext){
try {
//通用方法数据校验
ExcelImportValid.valid(patientExcelModel);
}catch (ExceptionCustom e){
System.out.println(e.getMessage());
//在easyExcel监听器中抛出业务异常
throw new ExcelAnalysisException(e.getMessage());
}
//TODO 业务代码
}
ExceptionCustom 类
package com.xxx.utils.exception;
import lombok.Data;
import lombok.EqualsAndHashCode;
/**
* 自定义注解异常
*
* @author DoLaLi
*/
@EqualsAndHashCode(callSuper = true)
@Data
public class ExceptionCustom extends RuntimeException
{
private static final long serialVersionUID = 1L;
public ExceptionCustom()
{
}
/**
* 错误编码
*/
private String errorCode;
/**
* 消息是否为属性文件中的Key
*/
private boolean propertiesKey = true;
/**
* 构造一个基本异常.
*
* @param message
* 信息描述
*/
public ExceptionCustom(String message)
{
super(message);
}
/**
* 构造一个基本异常.
*
* @param errorCode
* 错误编码
* @param message
* 信息描述
*/
public ExceptionCustom(String errorCode, String message)
{
this(errorCode, message, true);
}
/**
* 构造一个基本异常.
*
* @param errorCode
* 错误编码
* @param message
* 信息描述
*/
public ExceptionCustom(String errorCode, String message, Throwable cause)
{
this(errorCode, message, cause, true);
}
/**
* 构造一个基本异常.
*
* @param errorCode
* 错误编码
* @param message
* 信息描述
* @param propertiesKey
* 消息是否为属性文件中的Key
*/
private ExceptionCustom(String errorCode, String message, boolean propertiesKey)
{
super(message);
this.setErrorCode(errorCode);
this.setPropertiesKey(propertiesKey);
}
/**
* 构造一个基本异常.
*
* @param errorCode
* 错误编码
* @param message
* 信息描述
*/
public ExceptionCustom(String errorCode, String message, Throwable cause, boolean propertiesKey)
{
super(message, cause);
this.setErrorCode(errorCode);
this.setPropertiesKey(propertiesKey);
}
/**
* 构造一个基本异常.
*
* @param message
* 信息描述
* @param cause
* 根异常类(可以存入任何异常)
*/
public ExceptionCustom(String message, Throwable cause)
{
super(message, cause);
}
}