使用 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)
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.