SpringBoot--获得对象属性为空值的属性名的工具类

原文网址:SpringBoot--获得对象属性为空值的属性名的工具类_IT利刃出鞘的博客-CSDN博客

简介

说明

        本文介绍获得对象的空值属性名的工具类。

        本工具可以获得某个对象中值为空(为null或者空字符串等)的属性名,然后可以用于作为BeanUtils.copyProperties的最后一个参数,不拷贝这些空的属性。

优点

        方便快捷。

相关网址

Spring--BeanUtils忽略空值拷贝--方法/实例_IT利刃出鞘的博客-CSDN博客_bean拷贝忽略空值

代码

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<String> 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);
    }
}

用法

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;
 
@RestController
public 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";
    }
}

猜你喜欢

转载自blog.csdn.net/feiying0canglang/article/details/126679570
今日推荐