public static Map<String, Object> bindObjectToMap(Object form) {
Field[] keys = form.getClass().getDeclaredFields();
Method[] methods = form.getClass().getDeclaredMethods();
Map<String, Method> getMethods = new HashMap<String, Method>();
String key = null;
String name = null;
for(Method one : methods) {
name = one.getName();
if(name.startsWith("set")) continue;
key = name.substring(3).toLowerCase();
getMethods.put(key, one);
}
Map<String, Object> ret = new HashMap<String, Object>();
Method method = null;
for(Field one : keys) {
name = one.getName();
name = name.replaceAll("_", "");
if(!getMethods.keySet().contains(name.toLowerCase())) continue;
method = getMethods.get(name.toLowerCase());
try {
Object result = method.invoke(form, new Object[]{ });
if(result != null) ret.put(name, result);
} catch (IllegalAccessException e) {
System.out.println(e.getMessage() + " : " + method.getName());
} catch (IllegalArgumentException e) {
System.out.println(e.getMessage() + " : " + method.getName());
} catch (InvocationTargetException e) {
System.out.println(e.getMessage() + " : " + method.getName());
}
}
return ret;
}
'개발 업무 > JAVA' 카테고리의 다른 글
back-end에서 Request parameter 추가 (0) | 2023.03.15 |
---|---|
첨부파일 다운로드 (0) | 2023.02.14 |
File을 MultipartFile로 변환 (0) | 2023.02.13 |
File을 MultipartFile로 변환 (0) | 2023.02.13 |
특정 Domain Class와 Json Request 를 맵핑 시켜 주기 (0) | 2023.02.13 |