title: How to deserialize a generic type with ObjectMapper Jackson
body: <p>I am struggling trying to simplify my code. I have a common oppperation that makes a request to an API and gets a JSON object. This <code>json</code> can be <code>Categories</code>, <code>Products</code> etc.  I am using jackson <code>ObjectMapper</code>.</p>
  
  <p>Currently I have a method for each request, but I want to simplify it in one method. For example.</p>
  
  <pre><code>myMethod(String Path, Here The class Type)
  </code></pre>
  
  <p>One of this common methods is:</p>
  
  <pre><code>public List&lt;Category&gt; showCategories() {
  
      HttpClient client = HttpClientBuilder.create().build();
      HttpGet getRequest = new HttpGet(Constants.GET_CATEGORY);
      getRequest.setHeader(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON);
      getRequest.setHeader(HttpHeaders.COOKIE, Login.getInstance().getToken());
  
      List&lt;Category&gt; data = null;
  
      HttpResponse response;
      try {
          response = client.execute(getRequest);
          data = Constants.JSON_MAPPER.readValue(response.getEntity().getContent(), new TypeReference&lt;List&lt;Category&gt;&gt;() {
          });
      } catch (IOException ex) {
          LOGGER.error("Error retrieving categories, " + ex.toString());
      }
      // TODO: Replace List&lt;category&gt; with Observable?
      return data;
  }
  </code></pre>
  
  <p>The one thing that changes across all methods  is the type of object to retrieve.</p>
  
  <p>It is possible to generalize the line</p>
  
  <pre><code>Constants.JSON_MAPPER.readValue(response.getEntity().getContent(), new TypeReference&lt;List&lt;Category&gt;&gt;() {
          });
  </code></pre>
  
  <p>To be</p>
  
  <pre><code>Constants.JSON_MAPPER.readValue(response.getEntity().getContent(), new TypeReference&lt;List&lt;T&gt;&gt;() {
          });
  </code></pre>
  
  <p>I've tried to add as a param to the method <code>Class&lt;T&gt; class</code>, as shown <a href="https://stackoverflow.com/a/13119233/1612432">here</a>, but I get the error <code>Cannot find symbol T</code></p>