title: More elegant way to handle nested REST resource calls in Spring?
body: <p>I am consuming an external REST api in my Spring controller and have to aggregate some calls to fill my model before I return it. Let's assume my model looks like this:</p>
  
  <pre><code>public class MyModel{
    public String id;
    public String field1;
    public String field2;
  }
  </code></pre>
  
  <p>The api I'm consuming offers one call to get a list of the Ids of all the available objects <code>GET /objects</code> and some of the requred fields.</p>
  
  <pre><code>[
      {
        "id": "some_id_1",
        "field1": "some_value_1"
      },
      {
        "id": "some_id_2",
        "field1": "some_value_2"
      }
  ]
  </code></pre>
  
  <p>The api now offers me a second call <code>GET /object/{id}</code> to get a version of the object with all fields.</p>
  
  <pre><code>//GET /object/some_id_1
  {
    "id": "some_id_1",
    "field1": "some_value_1",
    "field2": "some_other_value_1"
  }
  </code></pre>
  
  <p>It's not possible to get this expanded version of the objects in the list call.</p>
  
  <p>I'm using <code>RestTemplate</code> with a jackson and I was wondering if there is any better way to build a list of building an expanded version of this list than first calling then list method and then foreaching over all the Ids, calling the detail resources and appending the result to a list. Of course I know that there is no way I can reduce the actual number of rest calls, just wondering if theres a more elegant way to write it in Spring (maybe somehing with annotations etc).</p>
  
  <pre><code>List&lt;MyModel&gt; result = new List&lt;&gt;();
  MyModel[] models = restTemplate.getForObject("/objects", MyModel[].class);
  for(MyModel slimModel: models){
    MyModel fatModel = restTemplate.getForObject("/object/{id}", MyModel.class, slimModel.id);
    result.add(fatModel);
  }
  return result;
  </code></pre>