在接口攔截器中,執行spa
/** * * @param obj 待驗證的requestDTO * @param originMap 必填參數Map * @return map :isSuccess=true 成功 * isSuccess=false 失敗 */ public static Map validatorCheck(Object obj, Map<String,Object> originMap) { Map<String,Object> resultMap = new HashMap<>(); //object 轉 map if (obj == null) { return null; } Map<String, Object> paramsMap = new HashMap<>(); Field[] declaredFields = obj.getClass().getDeclaredFields(); for (Field field : declaredFields) { field.setAccessible(true); try { paramsMap.put(field.getName(), field.get(obj)); } catch (IllegalAccessException e) { e.printStackTrace(); resultMap.put(field.getName(),e.getMessage()); return resultMap; } field.setAccessible(false); } //驗證map for (Object key : originMap.keySet()) { //字符串 Object value=paramsMap.get(key); if(value instanceof String){ if(paramsMap.get(key)==null || paramsMap.get(key).equals("")){ resultMap.put(key.toString(),originMap.get(key)); } }else{ if(paramsMap.get(key)==null){ resultMap.put(key.toString(),originMap.get(key)); } } } if(resultMap.size()>0){ resultMap.put("isSuccess",false); }else{ resultMap.put("isSuccess",true); } return resultMap; }
待續.....code