Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How Spring BeanUtils ignores null copies

2025-04-12 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how Spring BeanUtils ignores null copies". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn how to ignore null copies in Spring BeanUtils.

The package in which the BeanUtils class resides

Two packages provide the BeanUtils class:

Spring (recommended): org.springframework.beans.BeanUtilsApache: org.apache.commons.beanutils.BeanUtils

Ignore the use of null value copy attribute

BeanUtils.copyProperties (Object source, Object target, String... IgnoreProperties) get the null property name (utility class)

You can write your own utility class to get the property names of all the null in the object.

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) }} example

For the sake of comprehensiveness, the following situations are taken into account:

Inherit a class

An attribute is an Entity

Utility class 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

Basic 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: normal 1: locked 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;} Controllerpackage 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 practice"); / / blogRequest.setContent ("this article introduces the method of obtaining the field name of 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";}} Test

Visit: http://localhost:8080/test/

Backend result:

[updateTime, content]-- {"createTime": "2022-03-17 19:58:32", "updateTime": null, "deletedFlag": 0, "id": 10, "title": "Java actual combat", "content": null, "user": {"id": null, "userName": null, "nickName": null, "status": null}}

Conclusion

You can get the property name of the null of the parent class

Cannot get the property name of the null of the attribute

Other documents

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 SpringBoot 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 Thank you for your reading The above is the content of "how Spring BeanUtils ignores null copies". After the study of this article, I believe you have a deeper understanding of how Spring BeanUtils ignores null copies, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report