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

后端 未结 5 908
独厮守ぢ
独厮守ぢ 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:09

    public class AgentResponse {
    
    private T result;
    
    public AgentResponse(T result) {
        this.result = result;
    }
    public T getResult() {
        return result;
    }
    

    }

    So for the above class structure and T can be of Type T1,T2 class

    So to deserialize for AgentResponse, use the following code

    JavaType javaType = objectMapper.getTypeFactory.constructParametricType(AgentResponse.class,T1.class)
    AgentResponse agentResponseT1 = objectMapper.readValue(inputJson,javaType);
    

提交回复
热议问题