Spring BeanUtils如何忽略空值拷贝
发表于:2025-11-08 作者:千家信息网编辑
千家信息网最后更新 2025年11月08日,这篇文章主要讲解了"Spring BeanUtils如何忽略空值拷贝",文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习"Spring BeanUtils如何
千家信息网最后更新 2025年11月08日Spring BeanUtils如何忽略空值拷贝
这篇文章主要讲解了"Spring BeanUtils如何忽略空值拷贝",文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习"Spring BeanUtils如何忽略空值拷贝"吧!
BeanUtils类所在的包
有两个包都提供了BeanUtils类:
Spring的(推荐):org.springframework.beans.BeanUtilsApache的:org.apache.commons.beanutils.BeanUtils
忽略null值拷贝属性的用法
BeanUtils.copyProperties(Object source, Object target, String... ignoreProperties)
获取null属性名(工具类)
可以自己写一个工具类,用来获取对象里所有null的属性名字。
package com.example.util; import org.springframework.beans.BeanWrapper;import org.springframework.beans.BeanWrapperImpl;import java.beans.PropertyDescriptor;import java.util.HashSet;import java.util.Set;public class PropertyUtil { public static String[] getNullPropertyNames(Object source) { BeanWrapper src = new BeanWrapperImpl(source); PropertyDescriptor[] pds = src.getPropertyDescriptors(); Set emptyNames = new HashSet<>(); for (PropertyDescriptor pd : pds) { //check if value of this property is null then add it to the collection Object srcValue = src.getPropertyValue(pd.getName()); if (srcValue == null){ emptyNames.add(pd.getName()); } } String[] result = new String[emptyNames.size()]; return emptyNames.toArray(result); }} 示例
本处为了全面,将以下几种情况都考虑进去:
继承了某个类
某个属性是个Entity
工具类
package com.example.util; import org.springframework.beans.BeanWrapper;import org.springframework.beans.BeanWrapperImpl;import java.beans.PropertyDescriptor;import java.util.HashSet;import java.util.Set;public class PropertyUtil { public static String[] getNullPropertyNames(Object source) { BeanWrapper src = new BeanWrapperImpl(source); PropertyDescriptor[] pds = src.getPropertyDescriptors(); Set emptyNames = new HashSet<>(); for (PropertyDescriptor pd : pds) { //check if value of this property is null then add it to the collection Object srcValue = src.getPropertyValue(pd.getName()); if (srcValue == null){ emptyNames.add(pd.getName()); } } String[] result = new String[emptyNames.size()]; return emptyNames.toArray(result); }} Entity
基础Entity
package com.example.entity; import com.fasterxml.jackson.annotation.JsonFormat;import lombok.Data;import java.time.LocalDateTime;@Datapublic class BaseEntity { @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone="GMT+8") private LocalDateTime createTime; private LocalDateTime updateTime; private Long deletedFlag;}User
package com.example.entity; import lombok.Data;@Datapublic class User { private Long id; private String userName; private String nickName; // 0:正常 1:被锁定 private Integer status;}Blog
package com.example.entity; import lombok.Data;import lombok.EqualsAndHashCode;@Data@EqualsAndHashCode(callSuper = true)public class Blog extends BaseEntity{ private Long id; private String title; private String content; private User user;}VO
package com.example.vo; import com.example.entity.BaseEntity;import com.example.entity.User;import lombok.Data;import lombok.EqualsAndHashCode;@Data@EqualsAndHashCode(callSuper = true)public class BlogRequest extends BaseEntity { private Long id; private String title; private String content; private User user;}Controller
package com.example.controller; import com.example.entity.Blog;import com.example.entity.User;import com.example.util.PropertyUtil;import com.example.vo.BlogRequest;import com.fasterxml.jackson.core.JsonProcessingException;import com.fasterxml.jackson.databind.ObjectMapper;import org.springframework.beans.BeanUtils;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RestController;import java.time.LocalDateTime;import java.util.Arrays;@RestControllerpublic class HelloController { @Autowired private ObjectMapper objectMapper; @GetMapping("/test") public String test() { BlogRequest blogRequest = new BlogRequest(); blogRequest.setId(10L); blogRequest.setTitle("Java实战"); // blogRequest.setContent("本文介绍获取null的字段名的方法"); blogRequest.setUser(new User()); blogRequest.setCreateTime(LocalDateTime.now()); // blogRequest.setCreateTime(LocalDateTime.now()); blogRequest.setDeletedFlag(0L); User user = new User(); user.setId(15L); user.setUserName("Tony"); // user.setNickName("Iron Man"); // user.setStatus(1); String[] nullPropertyNames = PropertyUtil.getNullPropertyNames(blogRequest); System.out.println(Arrays.toString(nullPropertyNames)); System.out.println("------------------------------"); Blog blog = new Blog(); BeanUtils.copyProperties(blogRequest, blog, nullPropertyNames); try { System.out.println(objectMapper.writeValueAsString(blog)); } catch (JsonProcessingException e) { e.printStackTrace(); } return "test success"; }}测试
访问:http://localhost:8080/test/
后端结果:
[updateTime, content]------------------------------{"createTime":"2022-03-17 19:58:32","updateTime":null,"deletedFlag":0,"id":10,"title":"Java实战","content":null,"user":{"id":null,"userName":null,"nickName":null,"status":null}}结论
可以获取父类的null的属性名
不可以获取属性的null的属性名
其他文件
pom.xml
4.0.0 org.springframework.boot spring-boot-starter-parent 2.3.0.RELEASE com.example demo_SpringBoot 0.0.1-SNAPSHOT demo_SpringBoot Demo project for Spring Boot 1.8 org.springframework.boot spring-boot-starter-web org.projectlombok lombok 1.16.20 provided org.springframework.boot spring-boot-maven-plugin 2.3.0.RELEASE
感谢各位的阅读,以上就是"Spring BeanUtils如何忽略空值拷贝"的内容了,经过本文的学习后,相信大家对Spring BeanUtils如何忽略空值拷贝这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是,小编将为大家推送更多相关知识点的文章,欢迎关注!
属性
拷贝
工具
学习
内容
实战
情况
两个
名字
基础
字段
对象
就是
思路
所在
文件
文章
方法
更多
知识
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
嵌入式软件开发绩效考核
马来西亚服务器价格
软件开发实用技术介绍
银行技能考试软件开发
服务器怎么安装后台
安装数据库时找不见盘符
网络安全心怎么看
贵州网络服务器机柜云服务器
韩国网络安全事故
督查网络安全宣传内容
bsp跟嵌入式软件开发
数据库now()代表什么意思
vb数据库查询后累加
路由宝刷机做服务器
网络安全手抄报如何制作
做外贸好还是软件开发好
遵化数据网络技术不二之选
软件开发与控制专业
小学生网络安全教育课件背景图
廊坊网络服务器机柜价格
软件开发过程中汇编通常是指
怎么查云服务器的地址
毛绒玩具商品分析软件开发
加强网络安全建设的意见
搞软件开发一般工资多少
税控服务器是什么
服务器连接状态不好怎么办
软件开发pd是什么
第四届网络安全等级保护技术大会
数据库系统的管理功能有哪些