Is Jackson really unable to deserialize json into a generic type?

后端 未结 5 907
独厮守ぢ
独厮守ぢ 2020-11-30 01:35

This is a duplicate question because the following questions are either messy or they are not answered at all:

deserializing-a-generic-type-with-jackson

jack

5条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-30 02:30

    If you programmatically pick up the java.lang.reflect.Type from for instance a method return type or a field, then it is easiest to use

    Type type = ...;
    ObjectMapper mapper = new ObjectMapper();
    JavaType javaType = mapper.getTypeFactory().constructType( type );
    Object value = mapper.readValue( json, javaType );
    

    A fully nested JavaType is created, so Controller>> will be deserialzed correctly.

提交回复
热议问题