首页 > 编程语言 > 详细

javaBean非空校验

时间:2020-08-27 17:01:22      阅读:128      评论:0      收藏:0      [点我收藏+]
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author heyt
 * @date 2020/8/27
 *
 */
@Slf4j
public class BeanNotNullUtil {
    public static List<String> validateProperty(Object validateObj) {
        return validateProperty(validateObj,(String[])null);
    }
    /**
     *
     * @param cValidateObj  校验的实体类
     * @param cIgnoreProperties 排除不需要校验的属性
     * @return
     */
    public static List<String> validateProperty(Object cValidateObj, String... cIgnoreProperties) {
        PropertyDescriptor[] targetPds = BeanUtils.getPropertyDescriptors(cValidateObj.getClass());
        List<String> ignoreList = (cIgnoreProperties != null ? Arrays.asList(cIgnoreProperties) : null);
        List<String> errList = new ArrayList<>();
        for (PropertyDescriptor targetPd : targetPds) {
            Method readMethod = targetPd.getReadMethod();
            if (readMethod != null && (ignoreList == null || !ignoreList.contains(targetPd.getName()))) {
                try {
                    if (!Modifier.isPublic(readMethod.getDeclaringClass().getModifiers())) {
                        readMethod.setAccessible(true);
                    }
                    Object value = readMethod.invoke(cValidateObj);
                    if (value instanceof String) {
                        if (StringUtils.isEmpty((String) value)) {
                            errList.add(cValidateObj.getClass().getSimpleName()+ "." + targetPd.getName() + "不可为空");
                            continue;
                        }
                    }
                    if (value == null) {
                        errList.add(cValidateObj.getClass().getSimpleName() + "." + targetPd.getName() + "不可为空");
                     }

                } catch (Throwable ex) {
                    log.info(ex.getMessage());
                }
            }
        }
        return errList;
    }

    public static void main(String[] args) {
//        OrderInfoVO tOrderInfoVO = new OrderInfoVO();
//        tOrderInfoVO.setOrderAmount("123123");
//        tOrderInfoVO.setOrderNo("ijnuhygv");
//        tOrderInfoVO.setShopNo("asdfedc");
////        String[] strs = {"insureTransaction","shopName","transactionType","transactionTime","transactionNo","transactionDetail"};
//        String[] strs = {"transactionType","transactionTime","transactionDetail",""};
//
//        List<String> tLists = validateProperty(tOrderInfoVO,strs);
//        System.out.print(tLists.toString());
    }
}

 

javaBean非空校验

原文:https://www.cnblogs.com/heyt/p/13572108.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!