object轉(zhuǎn)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相關(guān)版本不兼容
我使用的是Spring5.0.8版本旨巷,把本地jsckson版本升到2.7.5以上或者更高即可