title: Deserialise a generic class , with unknown generic type - Jackson
body: <pre><code>public class MessageModel&lt;T&gt; {
      T body;
  }
  </code></pre>
  <p>Now <code>T</code> could be object of type say <code>Cricket</code> or <code>Football</code>. Now this class is serialised and published to a queue.
  While consuming, what is the best way to deserialise and identify the message type as Cricket or Football, so that the corresponding service is called. I am using jackson to serialise.</p>
  <p>I tried introducing a new property <code>Type</code> during serialisation and during deserialisation will first read as json object, identify the Type and again use jackson to deserialise as the type is known.
  Ex:</p>
  <pre><code>JSONObject jsonObject = new JSONObject(message);
  String messageType = jsonObject.optString(&quot;message_type&quot;);
  objectMapper.readValue(message,new TypeReference&lt;MessageModel&lt;messageType&gt;&gt;(){});
  </code></pre>
  <p>But looking for better approaches.</p>