简体   繁体   English

如何从某种类型的列表中获取 Jackson TypeReference<some class> ?</some>

[英]How to get a Jackson TypeReference from certain type of List<some class>?

I know how to get a TypeReference from known classes, such as:我知道如何从已知类中获取 TypeReference,例如:

        List<MyPojo> list = buildMyPojoList();
        String json = getObjectMapper().writeValueAsString(list);
        getObjectMapper().readValue(json, new TypeReference<List<MyPojo>>(){});

But when I get something like <Class<? extend Pojo>>但是当我得到类似<Class<? extend Pojo>> <Class<? extend Pojo>> rather than known class <MyPojo> , is it still possible to get a TypeReference like above? <Class<? extend Pojo>>而不是 known class <MyPojo> ,是否仍然可以获得像上面那样的 TypeReference? Or any other way to do readValue() given only the string and the class?或者仅给定字符串和 class 来执行readValue()的任何其他方法? (the json string contain info of a list of that given class) (json 字符串包含该给定类列表的信息)

Alright, found the answer by myself...好吧,自己找到了答案......

Could use things like:可以使用类似的东西:

    objectMapper.getTypeFactory().constructParametricType(List.class, Class<?> myClass)

not sure whether working or not, still trying...不确定是否有效,仍在尝试...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM