title: spring boot rest api return list of objects as json
body: <p>I am using the spring boot version 2.3.3, below is my pom.xml.</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  &lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
      xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;
      &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
      &lt;parent&gt;
          &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
          &lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
          &lt;version&gt;2.3.3.RELEASE&lt;/version&gt;
          &lt;relativePath/&gt; &lt;!-- lookup parent from repository --&gt;
      &lt;/parent&gt;
      &lt;groupId&gt;com.group&lt;/groupId&gt;
      &lt;artifactId&gt;artifact&lt;/artifactId&gt;
      &lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
      &lt;name&gt;project&lt;/name&gt;
      &lt;description&gt;Demo project for Spring Boot&lt;/description&gt;
  
      &lt;properties&gt;
          &lt;java.version&gt;11&lt;/java.version&gt;
      &lt;/properties&gt;
  
      &lt;dependencies&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-data-jdbc&lt;/artifactId&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-data-jpa&lt;/artifactId&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-devtools&lt;/artifactId&gt;
              &lt;scope&gt;runtime&lt;/scope&gt;
              &lt;optional&gt;true&lt;/optional&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.postgresql&lt;/groupId&gt;
              &lt;artifactId&gt;postgresql&lt;/artifactId&gt;
              &lt;scope&gt;runtime&lt;/scope&gt;
          &lt;/dependency&gt;
          &lt;!-- https://mvnrepository.com/artifact/org.json/json --&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.json&lt;/groupId&gt;
              &lt;artifactId&gt;json&lt;/artifactId&gt;
              &lt;version&gt;20190722&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-test&lt;/artifactId&gt;
              &lt;scope&gt;test&lt;/scope&gt;
              &lt;exclusions&gt;
                  &lt;exclusion&gt;
                      &lt;groupId&gt;org.junit.vintage&lt;/groupId&gt;
                      &lt;artifactId&gt;junit-vintage-engine&lt;/artifactId&gt;
                  &lt;/exclusion&gt;
              &lt;/exclusions&gt;
          &lt;/dependency&gt;
      &lt;/dependencies&gt;
  
      &lt;build&gt;
          &lt;plugins&gt;
              &lt;plugin&gt;
                  &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
                  &lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;
              &lt;/plugin&gt;
          &lt;/plugins&gt;
      &lt;/build&gt;
  
  &lt;/project&gt;
  </code></pre>
  <p>I have a custom class/model.</p>
  <pre><code>public class Summary {
      // attributes
      //constructor
      // getters/setters for each attributes
  }
  </code></pre>
  <p>Its repository (interface and implementation)</p>
  <pre><code>public interface SummaryRepository {
      List&lt;Summary&gt; getSummaryData(Date date);
  }
  
  @Repository
  @Transactional
  public class SummaryRepositoryImpl implements SummaryRepository {
  
      @PersistenceContext
      EntityManager entityManager;
  
  
      @Override
      public List&lt;Summary&gt; getSummaryData(Date date) {
          // Implementation
      }
  }
  </code></pre>
  <p>In the controller, I am having the following code:</p>
  <pre><code>@RestController
  public class SummaryController {
  
      @Autowired
      private SummaryRepository summaryRepository;
  
  
      @RequestMapping(
              value =&quot;/summary-data&quot;,
              method = RequestMethod.POST,
              consumes = &quot;application/json&quot;, 
              produces = MediaType.APPLICATION_JSON_VALUE
  
      )
      public ResponseEntity&lt;List&lt;Summary&gt;&gt; getSummaryData(@RequestBody RequestConfig requestConfig ) {
          String dateStr = requestConfig.getDate();
          Date date = new Date();
          SimpleDateFormat formatter = new SimpleDateFormat(&quot;yyyy-MM-dd&quot;, Locale.US);
          try {
              date = formatter.parse(dateStr);
          } catch (Exception e) {
              System.out.println(&quot;Exception: &quot; + e.getMessage());
          }
          // Some logic
          List&lt;Summary&gt; summaries =  summaryRepository.getSummaryData(date);
  
          if (summaries.size() &gt; 0 ) {
              return ResponseEntity.ok(summaries);
          } else {
              return ResponseEntity.notFound().build();
          }
      }
  
  }
  </code></pre>
  <p><em><strong>When I go to postman and send the POST request to above</strong></em>, I get the output as below:</p>
  <pre><code>[[&quot;2020-07-31&quot;, &lt;values&gt;],[&quot;2020-07-31&quot;,&lt;values2&gt;],[&quot;2020-07-31&quot;, &lt;values3&gt;],[&quot;2020-07-31&quot;,&lt;values4&gt;]]
  </code></pre>
  <p>i.e. literal list of object.</p>
  <p>I am looking to get it in Json; something like:</p>
  <pre><code>[
      { &quot;date&quot;: &quot;2020-07-31&quot;, &quot;attr2&quot;: &quot;value2&quot;, &quot;attr3&quot;: &quot;value3&quot; },
      { &quot;date&quot;: &quot;2020-07-31&quot;, &quot;attr2&quot;: &quot;value21&quot;, &quot;attr3&quot;: &quot;value31&quot; },
      { &quot;date&quot;: &quot;2020-07-31&quot;, &quot;attr2&quot;: &quot;value12&quot;, &quot;attr3&quot;: &quot;value13&quot; },
      { &quot;date&quot;: &quot;2020-07-31&quot;, &quot;attr2&quot;: &quot;value2&quot;, &quot;attr3&quot;: &quot;value3&quot; }
  ]
  </code></pre>
  <p>i.e. &quot;date&quot;, &quot;attr2&quot;, etc are my attributes of the Summary (already defined with getters/setters).</p>
  <p>What is missing here?</p>
  <p>--------<strong>UPDATE</strong>---------</p>
  <p>Based on the below response I have my SummaryResponse class.</p>
  <pre><code>import com.fasterxml.jackson.annotation.JsonProperty;
  
  import java.util.List;
  
  public class SummaryResponse {
  
      @JsonProperty(value = &quot;summary&quot;, required = true)
      List&lt;Summary&gt; summaryList;
  
      public SummaryResponse() {
      }
  
      public List&lt;Summary&gt; getSummaryList() {
          return summaryList;
      }
  
      public void setSummaryList(List&lt;Summary&gt; summaryList) {
          this.summaryList = summaryList;
      }
  }
  </code></pre>
  <p>I changed my controller logic to return this object.</p>
  <pre><code>@RequestMapping(
              value =&quot;/summary-data&quot;,
              method = RequestMethod.POST,
              consumes = &quot;application/json&quot;
  
      )
      public ResponseEntity&lt;SummaryResponse&gt; getSummaryData(@RequestBody RequestConfig requestConfig ) {
  
    // Logic
    List&lt;Summary&gt; summaries =  summaryRepository.getSummaryData(date);
    SummaryResponse summaryResponse = new SummaryResponse();
    summaryResponse.setSummaryList(summaries);
    if (summaries.size() &gt; 0 ) {
       return new ResponseEntity&lt;&gt;(summaryResponse, HttpStatus.OK);
    } else {
       return ResponseEntity.notFound().build();
    }
  }
  </code></pre>
  <p>Still I am not getting the JSON output. It is still Array of attributes.</p>
  <pre><code>{&quot;summary&quot;:[[&quot;2020-07-31&quot;,&quot;attr2_value&quot;,&quot;attr3_value&quot;, null, &quot;attr5_value&quot;] ,[&quot;2020-07-31&quot;,&quot;attr2_value&quot;,&quot;attr3_value&quot;,&quot;attr4_value&quot;,&quot;attr5_value&quot;]]}
  </code></pre>
  <p>What exactly is the issue here?</p>
  <p>I am looking something like below:</p>
  <pre><code>[
      { &quot;date&quot;: &quot;2020-07-31&quot;, &quot;attr2&quot;: &quot;attr2_value&quot;, &quot;attr3&quot;: &quot;attr3_value&quot;, &quot;attr4&quot;, null, &quot;attr5&quot;: &quot;attr5_value&quot; },
      { &quot;date&quot;: &quot;2020-07-31&quot;, &quot;attr2&quot;: &quot;attr2_value&quot;, &quot;attr3&quot;: &quot;attr3_value&quot;, &quot;attr4&quot;, &quot;attr4_value&quot;, &quot;attr5&quot;: &quot;attr5_value&quot; }
  ]
  </code></pre>
  <p>------------------<strong>UPDATE 2</strong>: <strong>Found the issue.</strong>----------------</p>
  <p>Thanks for the hints, I got the issue.</p>
  <p>In my Repository, I am using the native query where class is not entity.</p>
  <pre><code>List&lt;Summary&gt; summaryList = query.getResultList();
  return summaryList;
  </code></pre>
  <p>Although I am taking the result in summaryList, it is actually a Object[] and not List. (Didn't knew this, new to spring boot and JPA).</p>
  <p>So although my Controller is passing this (summaryList) to response it is actually a Object[].</p>
  <p>My Summary Class is not a Entity (does not come from table or has identity), so changed my repository method:</p>
  <pre><code>List&lt;Object&gt; result = (List&lt;Object&gt;) query.getResultList();
      Iterator it = result.iterator();
      List&lt;Summary&gt; summaryList = new ArrayList&lt;&gt;();
  
      while (it.hasNext()) {
          Object[] row = (Object[]) it.next();
          Summary summary = new Summary();
          summary.setDate( (Date) row[0]);
          summary.setName(String.valueOf(row[1]));
          summary.setAccount(String.valueOf(row[2]));
          ...
          ...
          // and so on
          summaryList.add(summary);
      }
  </code></pre>
  <p>Finally it is working as expected.</p>