java校验对象中的属性值是否符合规定条件

2021-10-18  本文已影响0人  楼兰King

以下是校验EndingComputeDTO 实体类中,四个属性值是否为空,输出为
[],达到效果。

import com.alibaba.fastjson.JSON;
import com.softium.datacenter.paas.domain.common.ConstantCacheMap;
import com.softium.datacenter.paas.domain.dto.EndingComputeDTO;
import com.softium.datacenter.paas.domain.entity.*;
import com.softium.framework.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.FatalBeanException;

import java.beans.PropertyDescriptor;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.math.BigDecimal;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class ListMapCommonUtils {
  /**校验bean中属性是否为空
     * @param validateObj  要校验的实体类
     * @param notIgnoreProperties 所要校验的字段
     * */
    public static List<String> validateProperty(Object validateObj,String... notIgnoreProperties) {
        PropertyDescriptor[] targetPds = BeanUtils.getPropertyDescriptors(validateObj.getClass());
        List<String> ignoreList = (notIgnoreProperties != null ? Arrays.asList(notIgnoreProperties) : 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(validateObj);
                    if (value instanceof String) {
                        if (StringUtils.isEmpty((String) value)) {
                            errList.add(validateObj.getClass().getSimpleName()+ "属性" + targetPd.getName() + "不能为空");
                            continue;
                        }
                    }
                    if (value instanceof Float || value instanceof Integer) {
                        if (StringUtils.isEmpty(value.toString())) {
                            errList.add(validateObj.getClass().getSimpleName()+ "属性" + targetPd.getName() + "不能为空");
                            continue;
                        }
                    }
                    if (value == null) {
                        errList.add(validateObj.getClass().getSimpleName() + "属性" + targetPd.getName() + "不能为空");
                    }
                } catch (Throwable ex) {
                    throw new FatalBeanException(
                            "Could not copy property '" + targetPd.getName() + "' from source to target", ex);
                }
            }
        }
        return errList;
    }

    public static void main(String[] args) {
        EndingComputeDTO dto=new EndingComputeDTO();
        dto.setToInstitutionAttachForBranch("a");
        dto.setToInstitutionAttachForCommerceTrust("a");
        dto.setToInstitutionAttachForPharmacy("a");
        dto.setToInstitutionAttachForVisual("a");
        List<String> list= validateProperty(dto,"toInstitutionAttachForPharmacy","toInstitutionAttachForVisual","toInstitutionAttachForBranch","toInstitutionAttachForCommerceTrust");
        System.out.println(JSON.toJSONString(list));

    }
}
上一篇下一篇

猜你喜欢

热点阅读