Note that there are some explanatory texts on larger screens.

plurals
  1. POIs Jackson really unable to deserialize json into a generic type?
    primarykey
    data
    text
    <p>This is a duplicate question because the following questions are either messy or they are not answered at all:</p> <p><a href="https://stackoverflow.com/questions/13118798/deserializing-a-generic-type-with-jackson">deserializing-a-generic-type-with-jackson</a></p> <p><a href="https://stackoverflow.com/questions/12355742/jackson-deserialize-into-runtime-specified-class">jackson-deserialize-into-runtime-specified-class</a></p> <p><a href="https://stackoverflow.com/questions/11664894/jackson-deserialize-using-generic-class">jackson-deserialize-using-generic-class</a></p> <p><a href="https://stackoverflow.com/questions/11659844/jackson-deserialize-generic-class-variable">jackson-deserialize-generic-class-variable</a></p> <p>I hope that this question will finally find an answer that makes this clear for good.</p> <p>Having a model :</p> <pre><code>public class AgentResponse&lt;T&gt; { private T result; public AgentResponse(T result) { this.result = result; } public T getResult() { return result; } } </code></pre> <p>JSON input:</p> <pre><code>{"result":{"first-client-id":3,"test-mail-module":3,"third-client-id":3,"second-client-id":3}} </code></pre> <p>and two recommended ways of deserializing generic types :</p> <pre><code>mapper.readValue(out, new TypeReference&lt;AgentResponse&lt;Map&lt;String, Integer&gt;&gt;&gt;() {}); </code></pre> <p>or</p> <pre><code>JavaType javaType = mapper.getTypeFactory().constructParametricType(AgentResponse.class, Map.class); mapper.readValue(out, javaType); </code></pre> <p>Jackson is never able to deal with the generic type T, it figures it's a Map from JavaType, but it finds Object type constructor argument because of type erasure and throws an error. So is this a Jackson bug, or am I doing something wrong? What else is explicit specification of TypeReference or JavaType for?</p> <pre><code>com.fasterxml.jackson.databind.JsonMappingException: No suitable constructor found for type [simple type, class com.fg.mail.smtp.AgentResponse&lt;java.util.Map&lt;java.lang.String,java.lang.Integer&gt;&gt;]: can not instantiate from JSON object (need to add/enable type information?) at [Source: java.io.InputStreamReader@4f2d26d; line: 1, column: 2] at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:164) at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromObjectUsingNonDefault(BeanDeserializerBase.java:984) at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:276) at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:121) at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:2888) at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2064) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload