package com.framework.util; import java.beans.PropertyDescriptor; import java.lang.reflect.Field; import java.lang.reflect.Method; import org.apache.commons.lang3.StringUtils; import com.framework.annotation.NotNull; /** * 自定义bean 验证 * @author 61004 * */ public class ValidatorUtil { /** * 验证非空 * @param obj * @return */ @SuppressWarnings("all") public static boolean notNull(Object obj) { boolean result = true; Class cls = obj.getClass(); NotNull notNullValidator = null; try { Field[] fields = cls.getDeclaredFields(); for (Field field : fields) { notNullValidator = field.getAnnotation(NotNull.class); if (notNullValidator != null) { PropertyDescriptor pd = new PropertyDescriptor(field.getName(), cls); Method getMethod = pd.getReadMethod();// 获得get方法 if(getMethod.invoke(obj) == null) { return false; }else{ String fieldVal = getMethod.invoke(obj).toString(); if (StringUtils.isBlank(fieldVal)) { result = false; return result; } } } } } catch (Exception e) { e.printStackTrace(); } return result; } }