当前位置: 首页 > news >正文

Java反射获取实例并填充注解值

文章目录

    • 1.场景
    • 2.普通实现方式
    • 3. 利用反射+注解设置值方式
      • 3.1 业务代码
      • 3.2 业务实体
      • 3.3 工具方法


1.场景

下载导入模版

2.普通实现方式

实现很简单,看着不那么优雅

  @GetMapping("/export-detail-template")@Operation(summary = "导出订单需求单明细导入模版 Excel")@OperateLog(type = EXPORT)public void exportTemplate(HttpServletResponse response) throws IOException {final DemandDetailImportTemplateVO templateVO = new DemandDetailImportTemplateVO();templateVO.setSeq(1);templateVO.setCustomerName("黄金大爷");templateVO.setSaleMan("张三");templateVO.setSkuNo("ES01BC0010");templateVO.setProductName("福叠(蝴蝶)");templateVO.setCategoryName("耳饰");templateVO.setBaseWeight("50");templateVO.setWeightSpan("40-50");templateVO.setNum(3);templateVO.setQualityRequired("足金999");templateVO.setPrintContent("无");templateVO.setCreateTime("2023/1/7  15:12:13");templateVO.setTimeRequired("2023/2/7  15:12:13");templateVO.setGuessWeight("49.99");templateVO.setRemark("无备注");ExcelUtils.write(response, "订单需求单明细导入模版.xls", "数据",DemandDetailImportTemplateVO.class,Collections.singletonList(templateVO));}

3. 利用反射+注解设置值方式

3.1 业务代码

@GetMapping("/export-detail-template")@Operation(summary = "导出订单需求单明细导入模版 Excel")@OperateLog(type = EXPORT)public void exportTemplate(HttpServletResponse response) {try {ExcelUtils.write(response, "订单需求单明细导入模版.xls", "数据", DemandDetailImportTemplateVO.class,Collections.singletonList(ReflUtil.getInstanceFillAnnotationValue(DemandDetailImportTemplateVO.class)));} catch (Exception e) {log.error("下载导入模版异常::" + e.getMessage());throw new ServiceException("下载导入模版异常");}}

3.2 业务实体

@Data
@Builder
@Accessors(chain = false)
public class DemandOrderImportVO {@ExcelIgnoreprivate Integer req;@ExcelProperty("客户名称")@Schema(description = "客户名称", example = "黄金大爷")private String customerName;@ExcelProperty("业务")@Schema(description = "业务", example = "张三")private String saleMan;@ExcelProperty("款式编码")@Schema(description = "款式编码", example = "ES01BC0010")private String skuNo;@ExcelProperty("产品图片")@Schema(description = "产品图片")private URL imageUrl;@ExcelProperty("产品名称")@Schema(description = "产品名称", example = "福叠(蝴蝶)")private String productName;@ExcelProperty("品类")@Schema(description = "品类", example = "耳饰")private String categoryName;@ExcelProperty("样板克重")@Schema(description = "样板克重", example = "50")private String baseWeight;@ExcelProperty("克重范围")@Schema(description = "克重范围", example = "40-50")private String weightSpan;@ExcelProperty("件数")@Schema(description = "件数", example = "3")private Integer num;@ExcelProperty("成色要求")@Schema(description = "成色要求", example = "足金999")private String qualityRequired;@ExcelProperty("字印要求")@Schema(description = "字印要求", example = "无")private String printContent;@ExcelProperty("下单时间")@Schema(description = "下单时间", example = "2023/1/7  15:12:13")private String createTime;@ExcelProperty("交期要求")@Schema(description = "交期要求", example = "2023/2/7  15:12:13")private String timeRequired;@ExcelProperty("预估克重")@Schema(description = "预估克重", example = "49.99")private String guessWeight;@ExcelProperty("备注")@Schema(description = "备注", example = "无备注")private String remark;
}

3.3 工具方法

注意:目前值考虑了 Integer与string的字段对象

public class ReflUtil extends ReflectUtil {/*** 反射获取对象并根据Schema注解的example进行值填充** @param tClass* @param <T>* @return* @throws Exception*/public static <T> T getInstanceFillAnnotationValue(Class<T> tClass) throws Exception {final T instance = tClass.newInstance();final Field[] fields = ReflectUtil.getFields(tClass);for (final Field field : fields) {final Class<?> filedType = field.getType();if (filedType != Integer.class && filedType != String.class) {continue;}final Schema annotation = field.getAnnotation(Schema.class);if (ObjUtil.isNull(annotation)) {continue;}final String example = annotation.example();field.setAccessible(true);field.set(instance, filedType == Integer.class ? Integer.valueOf(example) : example);}return instance;}
}

相关文章:

  • 2022 年全国职业院校技能大赛高职组云计算赛项试卷部分解析
  • 对象的复制
  • 【Android Studio】APP练手小项目——切换图片APP
  • 2024开放式耳机测评推荐榜单曝光!超热门开放式耳机选购攻略
  • C++牛客知识点3
  • kubectlkubeletrancherhelmkubeadm这几个命令行工具是什么关系?
  • vue项目心得(复盘)
  • 使用群晖docker将小爱音箱接入chatgpt
  • 玩转贝启科技BQ3588C开源鸿蒙系统开发板 —— 编译构建及此过程中的踩坑填坑(3)
  • Python用法:if __name__ == “__main__“的作用
  • 【漏洞复现】锐捷RG-UAC统一上网行为管理系统信息泄露漏洞
  • 【数据库系统概念】第三章 SQL语句(下)~重要!!!
  • oracle重启数据库lsnrctl重启监听
  • TDengine 签约西电电力
  • DevExpress历史安装文件包集合
  • 深入了解以太坊
  • .pyc 想到的一些问题
  • 【162天】黑马程序员27天视频学习笔记【Day02-上】
  • 【399天】跃迁之路——程序员高效学习方法论探索系列(实验阶段156-2018.03.11)...
  • AHK 中 = 和 == 等比较运算符的用法
  • iOS小技巧之UIImagePickerController实现头像选择
  • JS字符串转数字方法总结
  • Linux gpio口使用方法
  • Linux编程学习笔记 | Linux IO学习[1] - 文件IO
  • PyCharm搭建GO开发环境(GO语言学习第1课)
  • spring + angular 实现导出excel
  • SpringBoot几种定时任务的实现方式
  • vue-loader 源码解析系列之 selector
  • 记录:CentOS7.2配置LNMP环境记录
  • 码农张的Bug人生 - 初来乍到
  • 深度学习在携程攻略社区的应用
  • 微信小程序设置上一页数据
  • 想晋级高级工程师只知道表面是不够的!Git内部原理介绍
  • No resource identifier found for attribute,RxJava之zip操作符
  • 专访Pony.ai 楼天城:自动驾驶已经走过了“从0到1”,“规模”是行业的分水岭| 自动驾驶这十年 ...
  • ​​快速排序(四)——挖坑法,前后指针法与非递归
  • # C++之functional库用法整理
  • #Lua:Lua调用C++生成的DLL库
  • $GOPATH/go.mod exists but should not goland
  • (09)Hive——CTE 公共表达式
  • (6)【Python/机器学习/深度学习】Machine-Learning模型与算法应用—使用Adaboost建模及工作环境下的数据分析整理
  • (7)STL算法之交换赋值
  • (cos^2 X)的定积分,求积分 ∫sin^2(x) dx
  • (C语言)fread与fwrite详解
  • (html5)在移动端input输入搜索项后 输入法下面为什么不想百度那样出现前往? 而我的出现的是换行...
  • (笔试题)合法字符串
  • (附源码)spring boot公选课在线选课系统 毕业设计 142011
  • (附源码)spring boot火车票售卖系统 毕业设计 211004
  • (万字长文)Spring的核心知识尽揽其中
  • (原创)boost.property_tree解析xml的帮助类以及中文解析问题的解决
  • .NET 2.0中新增的一些TryGet,TryParse等方法
  • .NET 8 编写 LiteDB vs SQLite 数据库 CRUD 接口性能测试(准备篇)
  • .NET Reactor简单使用教程
  • .net下简单快捷的数值高低位切换
  • .NET应用架构设计:原则、模式与实践 目录预览