复杂excel表格内容数据导入 接口参数注解校验

news/2025/2/6 10:33:36/

校验代码 

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;// 格式正则表达式;}


http://www.ppmy.cn/news/1569769.html

相关文章

redis简介及应用

文章目录 1.redis简介2.安装配置2.1 单机部署2.2 配置 3 主从部署4 哨兵部署5.集群部署6.客户端工具 1.redis简介 某些网站出现的问题&#xff0c;如12306、淘宝等… 2.安装配置 2.1 单机部署 安装gcc、关闭防火墙、关闭selinux等 #安装gcc yum -y install gcc #关闭防火墙…

国产SiC碳化硅功率器件技术成为服务器电源升级的核心引擎

在服务器电源应用中&#xff0c;国产650V碳化硅&#xff08;SiC&#xff09;MOSFET逐步取代传统超结&#xff08;Super Junction, SJ&#xff09;MOSFET&#xff0c;其核心驱动力源于SiC材料在效率、功率密度、可靠性和长期经济性上的显著优势&#xff0c;叠加产业链成熟与政策…

React开发中箭头函数返回值陷阱的深度解析

React开发中箭头函数返回值陷阱的深度解析 一、箭头函数的隐式返回机制&#xff1a;简洁背后的规则二、块函数体中的显式返回要求&#xff1a;容易被忽视的细节三、真实场景下的案例分析案例1&#xff1a;忘记return导致组件渲染失败案例2&#xff1a;异步操作中的返回值陷阱 四…

吴恩达深度学习——卷积神经网络基础

本文来自https://www.bilibili.com/video/BV1FT4y1E74V&#xff0c;仅为本人学习所用。 文章目录 矩阵和张量边缘检测计算方式检测原理 Valid卷积和Same卷积卷积步长三维卷积单层卷积网络总结符号定义输入输出维度其他参数维度 举例 池化层示例输入层第一层卷积 - 池化第二层卷…

本地安装部署deepseek

在截图下载工具(地址不方便粘贴过不审核)复制安装程序链接下载模型管理工具ollama&#xff0c;下一步下一步&#xff0c;有需要也可以下载linux版的 githup&#xff1a;https://github.com/ollama/ollama/releases/tag/v0.5.7 安装程序&#xff1a;https://github.com/ollama…

java程序员面试自身优缺点,详细说明

程序员面试大厂经常被问到的Java异常机制问题,你搞懂了吗运行时异常:运行时异常是可能被程序员避免的异常。与检查性相反,运行时异常可以在编译时被忽略。错误(ERROR):错误不是异常,而是脱离程序员控制的问题。错误通常在代码中容易被忽略。例如:当栈溢出时,一个错误就发生了,它…

Vue 2 与 Vue 3 的主要区别

Vue.js 是一个流行的前端框架&#xff0c;用于构建用户界面和单页应用。自从 Vue 2 发布以来&#xff0c;社区对其进行了广泛的应用和扩展&#xff0c;而 Vue 3 的发布则带来了许多重要的改进和新特性。 性能提升 Vue 3 在响应式系统上进行了重大的改进&#xff0c;采用了基于…

Node.js 和 npm 安装教程

Node.js 和 npm 安装教程 Node.js 和 npm 安装教程什么是 Node.js 和 npm&#xff1f;Node.jsnpm 安装前的注意事项在 Windows 上安装 Node.js 和 npm步骤 1&#xff1a;访问 Node.js 官网步骤 2&#xff1a;选择适合的版本步骤 3&#xff1a;下载安装包步骤 4&#xff1a;运行…