private static ObjectMapper mapper;
public static ObjectMapper getObjectMapper(){
if(null == mapper){
mapper = new ObjectMapper().setVisibility(JsonMethod.FIELD, Visibility.ANY);
mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
}
return mapper;
}
/**
* 获取泛型的Collection Type
* @param collectionClass 泛型的Collection
* @param elementClasses 元素类
* @return JavaType Java类型
* @since 1.0
*/
public static Object convertToCollection(String jsonString ,Class<?> collectionClass, Class<?>... elementClasses) throws JsonParseException, JsonMappingException, IOException{
Object result = new Object();
JavaType javaType = getObjectMapper().getTypeFactory().constructParametricType(collectionClass, elementClasses);
result = getObjectMapper().readValue(jsonString, javaType);
return result;
}
mapper = new ObjectMapper().setVisibility(JsonMethod.FIELD, Visibility.ANY);
mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-asl</artifactId>
<version>1.9.13</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.13</version>
</dependency>