public class ObjectFieldIsNotNullUtil { public static void main(String[] args) { UserVO vo = new UserVO(); vo.setToken("1"); vo.setUserid("1"); boolean b = notNull(vo,"token"); System.out.println(b); } /** * 判斷一個對象的屬性有無值 * @param o * @param as 需要過濾的屬性,逗號分隔 * @return */ public static boolean notNull(Object o,String as){ try { for(Field f : o.getClass().getDeclaredFields()){ f.setAccessible(true); Object o1 = f.get(o); if(o1!=null && !"".equals(o1)&& !as.contains(f.getName())){ return true; } } }catch (Exception e){ return false; } return false; } }