V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
forbreak
V2EX  ›  Java

使用 Jackson 转换 json 问题 ,请叫高手指导。 实体类里面 有 <M extends TreeEntity> Collection<M> getChildren() {}这种类型的时候 转换报错!

  •  1
     
  •   forbreak · 2015-03-13 17:41:02 +08:00 · 5573 次点击
    这是一个创建于 3527 天前的主题,其中的信息可能已经有所发展或是发生改变。

    使用 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 条回复    2015-04-17 10:05:20 +08:00
    sorra
        1
    sorra  
       2015-04-17 10:05:20 +08:00
    把TypeReference换成TypeFactory.constructCollectionType(collectionClass, elementClass)
    因为泛型被擦除了,所以必须告诉Jackson你的元素类型M到底是啥具体类型。
    必须知道具体类型才能做JSON反序列化。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1041 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 22ms · UTC 19:41 · PVG 03:41 · LAX 11:41 · JFK 14:41
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.