object转Map
1.使用jackson
依赖
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.6.3</version>
</dependency>
使用
ObjectMapper oMapper=new ObjectMapper();
Map<String,Object>map=oMapper.convertValue(obj,Map.class);
2.使用apache的beanutils
依赖
<!-- https://mvnrepository.com/artifact/commons-beanutils/commons-beanutils -->
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.9.3</version>
</dependency>
使用
Map<Object, Object> introspected = new org.apache.commons.beanutils.BeanMap(object);
java.lang.NoClassDefFoundError: com/fasterxml/jackson/databind/exc/InvalidDefinitionException
此信息为spring和jackson相关版本不兼容
我使用的是Spring5.0.8版本,把本地jsckson版本升到2.7.5以上或者更高即可