首页 > 解决方案 > 杰克逊序列化通用但不反序列化

问题描述

我怎样才能使这项工作?我看看其他问题,但我无法弄清楚如何。其实我对杰克逊了解不多。也许“龙目岛”是问题的根源。先感谢您。

    @Data class A{String b;}
    @Data class B<T>{ T c; }


    A a1 = new A();
    a1.setB("sa");
    B<A> s = new B<A>();
    s.c = a1;

    ObjectMapper objectMapper = new ObjectMapper();

    String sj = objectMapper.writeValueAsString(s);
    System.out.println(sj); // {"c":{"b":"sa"}} 

    B<A> ba = objectMapper.readerFor(new TypeReference<B<A>>() {
    })
            .readValue(sj); // this is not working 

给出这个错误:

    com.fasterxml.jackson.databind.JsonMappingException: Cannot deserialize 
    EmbeddedTest$1B (of type local/anonymous) as a Bean.

标签: javajacksondeserialization

解决方案


推荐阅读