You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
udi-wms-java/src/main/java/com/glxp/api/util/EntityToMapConverter.java

36 lines
1.0 KiB
Java

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package com.glxp.api.util;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
public class EntityToMapConverter {
public static <T> Map<String, Object> convertEntityToMap(T entity) {
if (entity == null) {
return null;
}
Map<String, Object> result = new HashMap<>();
Class<?> clazz = entity.getClass();
// 获取所有字段包括私有字段但需要设置setAccessible(true)
Field[] fields = clazz.getDeclaredFields();
for (Field field : fields) {
field.setAccessible(true); // 设置为可访问,以便访问私有字段
try {
// 获取字段的值
Object value = field.get(entity);
result.put(field.getName(), value);
} catch (IllegalAccessException e) {
// 这通常不会发生因为我们已经调用了setAccessible(true)
e.printStackTrace();
}
}
return result;
}
}