Beans拷贝忽略空对象

网友投稿 555 2022-11-20

Beans拷贝忽略空对象

Beans拷贝忽略空对象

@Data@NoArgsConstructor@AllArgsConstructorpublic class Student { @ExcelProperty("name") private String name; @ExcelProperty("age") private Integer age; @JsonFormat( shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss" ) @ExcelProperty("birthday") private LocalDateTime birthday; private String birthdayStr; public Student(String name, Integer age, LocalDateTime birthday) { this.name = name; this.age = age; this.birthday = birthday; } public String getBirthdayStr() { System.out.println("------------getBirthdayStr--------------"); return birthday.toString(); }}

@Data@ToString@AllArgsConstructorpublic class StudentVo { @ExcelProperty("name") private String name; @ExcelProperty("age") private Integer age; @JsonFormat( shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss" ) @ExcelProperty("birthday") private LocalDateTime birthday; public StudentVo() { System.out.println("---------------vo construct---------------"); } private String birthdayStr; public StudentVo(String name, Integer age, LocalDateTime birthday) { this.name = name; this.age = age; this.birthday = birthday; } public String getBirthdayStr() { System.out.println("-----vo-------getBirthdayStr--------------"); return birthday.toString(); }}

Student student = new Student("2", 222, LocalDateTime.now()); student.setName(null); StudentVo studentVo = new StudentVo("2", 2, LocalDateTime.now()); studentVo.setBirthdayStr("sssss"); BeanUtils.copyProperties(student,studentVo,OrikaUtil.getNullPropertyNames(student)); System.out.println(studentVo.toString());

public static String[] getNullPropertyNames(Object source) { final BeanWrapper src = new BeanWrapperImpl(source); java.beans.PropertyDescriptor[] pds = src.getPropertyDescriptors(); Set emptyNames = new HashSet(); for (java.beans.PropertyDescriptor pd : pds) { Object srcValue = src.getPropertyValue(pd.getName()); if (srcValue == null) emptyNames.add(pd.getName()); } String[] result = new String[emptyNames.size()]; return emptyNames.toArray(result); }

在这里插入代码

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:NPM electron helloworld异常 2021-09-16 npm install @vue/cli 卡在了 reify:rxjs: timing reifyNode: node_mod
下一篇:redis知识整理详解大全
相关文章

 发表评论

暂时没有评论,来抢沙发吧~