title: Micronaut HTTP Client - Deserialize generic type - For API Testing
body: <p>For API testing I have to parse the response of a request using io.micronaut.http.client.HttpClient</p>
  
  <p>I prefer to use the format given below.</p>
  
  <pre class="lang-java prettyprint-override"><code>Response&lt;User&gt; response =
                  client.toBlocking().retrieve(HttpRequest.POST("/", user), Response.class);
  </code></pre>
  
  <p>But It is not working. I am getting type cast error while trying to fetch User from response</p>
  
  <pre class="lang-java prettyprint-override"><code>User user = response.getUser();
  </code></pre>
  
  <p>Currently I am using <em>ObjectMapper</em> (Jackson) like given below,</p>
  
  <pre class="lang-java prettyprint-override"><code>String response = client.toBlocking().retrieve(HttpRequest.POST("/", user), String.class);
  ObjectMapper objectMapper = new ObjectMapper();
  Response&lt;User&gt; response = objectMapper.readValue(response, new TypeReference&lt;Response&lt;User&gt;&gt;() {});
  
  </code></pre>
  
  <p>Is there any way to use <em>TypeReference</em> directly with <em>HttpClient</em> retrieve method? Or any other way to use generic class type with <em>HttpClient</em>.</p>
  
  <p>Other useful informations</p>
  
  <pre class="lang-java prettyprint-override"><code>// Response generic class
  class Response&lt;T&gt; {
  T data;
  ....
  }
  </code></pre>
  
  <p>Sample response</p>
  
  <pre><code>{
      "data": {
          "name":"sample"
      },
      "message": "Success"
  }
  </code></pre>