Jackson - Deserialize Generic class variable

前端 未结 5 687
梦谈多话
梦谈多话 2020-12-07 00:33

I had posted the question wrongly. I am posting the question correctly here ...

I am getting a json string as a HTTP response. I know the structure of it. It is as

5条回答
  •  生来不讨喜
    2020-12-07 01:05

    You will need to build JavaType explicitly, if generic type is only dynamically available:

    // do NOT create new ObjectMapper per each request!
    final ObjectMapper mapper = new ObjectMapper();
    
    public Json void deSerialize(Class clazz, InputStream json) {
        return mapper.readValue(json,
          mapper.getTypeFactory().constructParametricType(Json.class, clazz));
    }
    

提交回复
热议问题