0

我已经像这样定义了一个 HashMaps 树:

public class HashTree<K, V> extends HashMap<K, HashTree<K, V>> {

    private static final long serialVersionUID = 1L;
    boolean isLeaf = true;
    private HashMap<K, V> value = new HashMap<K, V>();

    public HashMap<K, V> getValues() {
        return value;
    }

    public void setValues(HashMap<K, V> value) {
        this.value = value;
    }

    public void putValue(K key, V value) {
        this.value.put(key, value);
    }

    public void removeValue(K key) {
        this.value.remove(key);
    }

    public void clearValues() {
        this.value.clear();
    }

}

注意类型是递归的:HashMap<K, HashTree<K, V>>. 在 Java 中这很好用,但是当我用 Jackson 序列化时,我得到了堆栈溢出:

java.lang.StackOverflowError
    com.fasterxml.jackson.databind.type.TypeBindings._resolveBindings(TypeBindings.java:232)
    com.fasterxml.jackson.databind.type.TypeBindings._resolveBindings(TypeBindings.java:269)
    com.fasterxml.jackson.databind.type.TypeBindings._resolveBindings(TypeBindings.java:309)
    com.fasterxml.jackson.databind.type.TypeBindings._resolveBindings(TypeBindings.java:309)
    com.fasterxml.jackson.databind.type.TypeBindings._resolveBindings(TypeBindings.java:309)
    com.fasterxml.jackson.databind.type.TypeBindings._resolve(TypeBindings.java:203)
    com.fasterxml.jackson.databind.type.TypeBindings.findType(TypeBindings.java:121)
    com.fasterxml.jackson.databind.type.TypeFactory._fromVariable(TypeFactory.java:798)
    com.fasterxml.jackson.databind.type.TypeFactory._constructType(TypeFactory.java:343)
    com.fasterxml.jackson.databind.type.TypeFactory.findTypeParameters(TypeFactory.java:279)
    com.fasterxml.jackson.databind.type.TypeFactory.findTypeParameters(TypeFactory.java:251)
    com.fasterxml.jackson.databind.type.TypeFactory._fromParamType(TypeFactory.java:759)
    com.fasterxml.jackson.databind.type.TypeFactory._constructType(TypeFactory.java:337)

...

我试图用这个来插入序列化,当它遇到哈希树时应该只返回一个字符串“123”。

public class HashTreeSerializer<K,V> extends StdSerializer<HashTree<K,V>> {

    public HashTreeSerializer() {
        super(HashTree.class, false);
    }

    @Override
    public void serialize(HashTree<K, V> tree, JsonGenerator json,
            SerializerProvider provider) throws IOException,
            JsonGenerationException {
        json.writeString("123");
    }

}

我什至将 HashTree 扩展到 StringHashTree 以删除那些泛型类型,但我仍然遇到堆栈溢出。有任何想法吗?

4

1 回答 1

2

这是一个错误;类型解析器应该能够将其解析为可接受的无变量类型。

于 2012-09-18T16:22:44.177 回答