title: Custom Mapping from JSON to Java/POJO in Spring RestTemplate response
body: <p>I have a Spring boot application where I am using RestTemplate to call a Rest API and I receive following JSON formatted response:</p>
  
  <pre><code>{
    "data": [
      {
        "id": "1",
        "type": "type1",
        "config": {
          "property1" : "value1",
          "property2" : "value2"
        }
      },
      {
        "id": "2",
        "type": "type2",
        "config": {
          "property3" : "value3",
          "property4" : "value4",
          "propArray": [ "element1", "element2"]
        }
      }
    ]
  }
  </code></pre>
  
  <p>The individual elements within array 'data' has few different structures (2 examples above) where I would like to map different Class Types with individual elements which depends on the value of the element 'type'.</p>
  
  <p>For example value 'type1' should map to an object of Class type 'Type1' and so on.</p>
  
  <p>I have Classes created as below:
  MyResponse:</p>
  
  <pre class="lang-java prettyprint-override"><code>public Class MyResponse {
    List&lt;Data&gt; data;
  ..
    \\getter and setters
  }
  </code></pre>
  
  <p>Data:</p>
  
  <pre class="lang-java prettyprint-override"><code>public Interface Data {}
  </code></pre>
  
  <p>Type1:</p>
  
  <pre class="lang-java prettyprint-override"><code>public Class Type1 implements Data {
    private String property1;
  
    private String property2;
  ..
    \\getter and setters
  }
  </code></pre>
  
  <p>Type2:</p>
  
  <pre class="lang-java prettyprint-override"><code>public Class Type1 implements Data {
    private String property3;
  
    private String property4;
  
    private List&lt;String&gt; propArray;
  ..
    \\getter and setters
  }
  </code></pre>
  
  <p>How can I map above conditional structure?</p>