使用 Jackson转换 json ,实体类里面含有 <M extends TreeEntity> Collection 类型的变量,进行字符串转化对象的时候报错。
实体类如下:
@JsonIgnoreProperties(ignoreUnknown = true)
public class OrgTree {
protected Collection<? extends OrgTree> children = new HashSet<OrgTree>();
@Override
public <M extends OrgTree> Collection<M> getChildren() {
return super.getChildren();
}
@Override
public <M extends OrgTree> void setChildren(Collection<M> children) {
super.setChildren(children);
}
}
</code>
</pre>
json 配置处理如下
private static ObjectMapper objectMapper = new ObjectMapper();
static {
objectMapper = new ObjectMapper();
Hibernate4Module hibernateModule = new Hibernate4Module();
hibernateModule.configure(Hibernate4Module.Feature.FORCE_LAZY_LOADING, false);
objectMapper.registerModule(hibernateModule);
objectMapper.configure(SerializationFeature.INDENT_OUTPUT, true);
objectMapper.configure(SerializationFeature.WRITE_NULL_MAP_VALUES, false);
objectMapper.configure(SerializationFeature.WRITE_EMPTY_JSON_ARRAYS, false);
objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false); // 允许序列化空的对象,比如Object
objectMapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
objectMapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
}
调用
objectMapper.readValue(json,OrgTree.class);
//尝试过多种方式 调用 如
readValue(json,new TypeReference<OrgTree>(){});
readValue(json,new TypeReference<Collection<OrgTree>>(){});
readValue(json,new TypeReference<Collection<? extends OrgTree>>(){});
错误信息 如下:
com.fasterxml.jackson.databind.JsonMappingException: Type variable 'M' can not be resolved (with context of class com.biz.framework.domain.OrgTree)
at com.fasterxml.jackson.databind.deser.DeserializerCache._createAndCache2(DeserializerCache.java:269)
at com.fasterxml.jackson.databind.deser.DeserializerCache._createAndCacheValueDeserializer(DeserializerCache.java:244)
at com.fasterxml.jackson.databind.deser.DeserializerCache.findValueDeserializer(DeserializerCache.java:143)
at com.fasterxml.jackson.databind.DeserializationContext.findContextualValueDeserializer(DeserializationContext.java:325)
at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.createContextual(CollectionDeserializer.java:147)
at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.createContextual(CollectionDeserializer.java:23)
at com.fasterxml.jackson.databind.DeserializationContext.findRootValueDeserializer(DeserializationContext.java:348)
at com.fasterxml.jackson.databind.ObjectMapper._findRootDeserializer(ObjectMapper.java:2899)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:2793)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:1950)
at com.biz.framework.json.JSON.toObject(JSON.java:112)
1
sorra 2015-04-17 10:05:20 +08:00
把TypeReference换成TypeFactory.constructCollectionType(collectionClass, elementClass)
因为泛型被擦除了,所以必须告诉Jackson你的元素类型M到底是啥具体类型。 必须知道具体类型才能做JSON反序列化。 |