title: Spring - REST Controller Return to return some specific fields in JSON
body: <p>I wanted to expose (though Controller) some specific fields of my Entity (let say <em>Person</em>), which has let say:</p>
  
  <ul>
  <li>id</li>
  <li>name</li>
  <li>number</li>
  <li>...</li>
  </ul>
  
  <p>And I wanted to prepare my REST controller, so it will return array of objects - but only id and name of objects:</p>
  
  <pre><code>[
    {
      "name": "name1",
      "id": 1
    },
    {
      "name": "name2",
      "id": 2
    }
    ...
  ]
  </code></pre>
  
  <p>I found something similar - <a href="https://stackoverflow.com/questions/30558784/spring-rest-controller-return-specific-fields">Spring Rest Controller Return Specific Fields</a> however the code behind it, is not looking well:</p>
  
  <pre><code>public @ResponseBody List&lt;Map&lt;String, Object&gt;&gt; getAll() {
      List&lt;Person&gt; allPersons = personService.findAll();
  
      List&lt;Map&lt;String, Object&gt;&gt; resultCollection = new ArrayList&lt;Map&lt;String, Object&gt;&gt;();
      for (Person person : allPersons) {
          Map&lt;String, Object&gt; personValuesMap = new HashMap&lt;String, Object&gt;();
          personValuesMap.put("id", person.getId());
          personValuesMap.put("name", person.getName());
          resultCollection.add(personValuesMap);
      }
      return resultCollection;
  }
  </code></pre>
  
  <p>Is there any more beauty/clear/spring way to achieve this?</p>