Java对象转Map<String,String>
Java对象转Map<String,String>
import org.springframework.beans.BeanUtils; import org.springframework.util.ReflectionUtils; private static Map<String, String> convertObjectToMap(Object obj){ return Arrays.stream(BeanUtils.getPropertyDescriptors(obj.getClass())) .filter(pd -> !"class".equals(pd.getName())) .collect(HashMap::new, (map, pd) -> { if(ReflectionUtils.invokeMethod(pd.getReadMethod(), obj) != null) { map.put(pd.getName(), String.valueOf(ReflectionUtils.invokeMethod(pd.getReadMethod(), obj))); } }, HashMap::putAll); }