title: Custom Jackson Deserialization of a Generic Abstract class
body: <p>I am having issues when trying to deserializing the following class:</p>
  
  <pre><code>public class MetricValuesDto {
  
      private Map&lt;MetricType, MetricValueDto&lt;?&gt;&gt; metricValues;
  
      public MetricValuesDto() {
      }
  
      public MetricValuesDto(Map&lt;MetricType, MetricValueDto&lt;?&gt;&gt; metricValues) {
          this.metricValues = metricValues;
      }
  
      public Map&lt;MetricType, MetricValueDto&lt;?&gt;&gt; getMetricValues() {
          return metricValues;
      }
  
      public void setMetricValues(Map&lt;MetricType, MetricValueDto&lt;?&gt;&gt; metricValues) {
          this.metricValues = metricValues;
      }
  }
  </code></pre>
  
  <p>My generic abstract class:</p>
  
  <pre><code>public abstract class MetricValueDto&lt;T&gt; {
  
      private T value;
      private MetricTrend trend;
  
      public MetricValueDto(T value, MetricTrend trend) {
          this.value = value;
          this.trend = trend;
      }
  
      public T getValue() {
          return value;
      }
  
      public void setValue(T value) {
          this.value = value;
      }
  
      public MetricTrend getTrend() {
          return trend;
      }
  
      public void setTrend(MetricTrend trend) {
          this.trend = trend;
      }
  }
  </code></pre>
  
  <p>I have two concrete classes which implement <code>MetricValueDto</code>:</p>
  
  <p>IntMetricValueDto:</p>
  
  <pre><code>public class IntMetricValueDto extends MetricValueDto&lt;Integer&gt; {
  
      public IntMetricValueDto(Integer value, MetricTrend trend) {
          super(value, trend);
      }
  }
  </code></pre>
  
  <p>FloatMetricValueDto:</p>
  
  <pre><code>public class FloatMetricValueDto extends MetricValueDto&lt;Float&gt; {
  
      public FloatMetricValueDto(Float value, MetricTrend trend) {
          super(value, trend);
      }
  }
  </code></pre>
  
  <p>Any idea of what's the correct strategy to deserialize MetricValueDto so I can parse it through <a href="https://fasterxml.github.io/jackson-databind/javadoc/2.3.0/com/fasterxml/jackson/databind/ObjectMapper.html" rel="nofollow noreferrer">ObjectMapper</a> or an <a href="https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/client/RestTemplate.html" rel="nofollow noreferrer">RestTemplate</a>? Whenever I run:</p>
  
  <pre><code>restTemplate.exchange("myEndpoint", HttpMethod.GET, entity, DataCollectionEventDto.class);
  </code></pre>
  
  <p>I get </p>
  
  <blockquote>
    <p>Caused by: com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of com.resson.dto.MetricValueDto: abstract types either need to be mapped to concrete types, have custom deserializer, or contain additional type information</p>
  </blockquote>
  
  <p>DataCollectionEventDto:</p>
  
  <pre><code>public class DataCollectionEventDto {
  
      private List&lt;MapLayerDto&gt; mapLayers;
  
      @JsonUnwrapped
      private MetricValuesDto metricValues;
  
      public List&lt;MapLayerDto&gt; getMapLayers() {
          return mapLayers;
      }
  
      public void setMapLayers(List&lt;MapLayerDto&gt; mapLayers) {
          this.mapLayers = mapLayers;
      }
  
      public MetricValuesDto getMetricValues() {
          return metricValues;
      }
  
      public void setMetricValues(MetricValuesDto metricValues) {
          this.metricValues = metricValues;
      }
  
      @Override
      public String toString() {
          return ToStringBuilder.reflectionToString(this);
      }
  }
  </code></pre>
  
  <p>I have basically tried everything on web and I could not make it work; any suggestion would be helpful.</p>