校验代码
java">// 校验文件内容List<CheckResult> checkResults = CheckResult.checkResultList(excelDataList);String collect = checkResults.stream().map(CheckResult::getMsg).collect(Collectors.joining(","));if (!CollectionUtils.isEmpty(checkResults)) {return Result.fail().setCode(2).setMessage(collect);}
utils包
java">package com.gkfx.farm.model.constant;import com.gkfx.farm.model.dto.NotNull;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import lombok.extern.log4j.Log4j2;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;@Data
@Log4j2
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
public class CheckResult {public static final String MSG_SYS_ERROR = "解析错误";public static final String MSG_REQUIRE = "必填";public static final String MSG_PARAM_ERROR = "参数错误";public static final String MSG_FORMAT_ERROR = "格式错误";public static final String REASON_REQUIRE = "不能为空";public static final String REASON_WX = "无效数据";public static final String REASON_COMMON_ERROR = "请仔细查看模板中的填写说明";public static final String REASON_IP_FORMAT_ERROR = "IP格式错误";public static final String REASON_PORT_FORMAT_ERROR = "格式错误";public static final String REASON_REGEX_ERROR = "只允许中文,数字,与大小写英文,特殊字符只允许包含.*,\\-_/#()";public static final String REGEX_DEFAULT = "^[\\u4e00-\\u9fa5a-zA-Z0-9 .*,\\-_/#()]*$";Integer lineNumber;String field;String reason;String msg;public static <T> List<CheckResult> check(T t) {List<CheckResult> res = new ArrayList<>();try {int lineNumber = -1;//获取对象所有属性集合Field[] fields = t.getClass().getDeclaredFields();//遍历所有属性for (Field field : fields) {field.setAccessible(Boolean.TRUE);if (field.getName().equals("lineNumber")) {lineNumber = ((int) field.get(t));}NotNull annotation = field.getAnnotation(NotNull.class);if (!ObjectUtils.isEmpty(annotation)) {boolean f = true;Object value = field.get(t);String cfName = annotation.cfName();String cfValue = annotation.cfValue();String regexValue = annotation.regexValue();String reason = annotation.reason();String message = annotation.message();boolean regexCheck = annotation.regex();if (StringUtils.hasText(cfName) && StringUtils.hasText(cfValue)) {Field field1 = t.getClass().getDeclaredField(cfName);field1.setAccessible(true);if (field1.getType() == Boolean.class) {if (field1.get(t) == null || !Boolean.valueOf(cfValue).equals(field1.get(t))) {f = false;}} else {if (field1.get(t) == null || !StringUtils.commaDelimitedListToSet(cfValue).contains(String.valueOf(field1.get(t)))) {f = false;}}}//判断该参数是否为空if (f && ObjectUtils.isEmpty(value)) {res.add(new CheckResult().setLineNumber(lineNumber).setField(annotation.name()).setMsg(ObjectUtils.isEmpty(message) ? MSG_REQUIRE : message).setReason(ObjectUtils.isEmpty(reason) ? REASON_REQUIRE : reason));} else {//判断输入内容是否包含特殊字符if (regexCheck) {Pattern pattern = Pattern.compile(regexValue);Matcher matcher = pattern.matcher(String.valueOf(value));if (!matcher.matches()) {res.add(new CheckResult()
// .setLineNumber(lineNumber)
// .setField(annotation.name()).setMsg(MSG_FORMAT_ERROR));
// .setReason(REASON_PORT_FORMAT_ERROR));}}}}}} catch (Exception e) {log.warn("解析数据异常", e);}return res;}public static <T> List<CheckResult> checkResultList(List<T> t) {List<CheckResult> res = new ArrayList<>();for (T t1 : t) {res.addAll(check(t1));}return res;}}
注解文件
java">package com.gkfx.farm.model.dto;import com.gkfx.farm.model.constant.CheckResult;import java.lang.annotation.*;@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface NotNull {String name() default ""; //字段名称String message() default CheckResult.MSG_REQUIRE;//错误信息String reason() default CheckResult.REASON_REQUIRE; // 错误原因String cfName() default ""; // 条件必填的字段名称String cfValue() default ""; // 条件必填的值,多个英文逗号隔开boolean regex() default false;// 是否进行格式判断;String regexValue() default CheckResult.REGEX_DEFAULT;// 格式正则表达式;}