Stream JSON output in Spring MVC
<p>My application is built using Spring Boot(1.3.3.RELEASE) with Spring MVC, Spring data JPA Hibernate. MySql is the database and Jackson is the JSON serializer. On java 8.</p>
<p>I want to return a huge data set in my controller method. Instead of retrieving all the data and then passing into the Jackson serializer, I want to return a stream of objects like below:</p>
<pre><code>@RequestMapping(value = &quot;/candidates/all&quot;, method = RequestMethod.GET)
public Stream&lt;Candidate&gt; getAllCandidates(){
    try { 
        return candidateDao.findAllByCustomQueryAndStream();
    } catch(Exception e){
        LOG.error(&quot;Exception in getCandidates&quot;,e);
    }
    return null;
}
</code></pre>
<p>my DAO is like below:</p>
<pre><code>@Query(&quot;select c from Candidate c&quot;)
public Stream&lt;Candidate&gt; findAllByCustomQueryAndStream();
</code></pre>
<p>However, Jackson is serializing the stream object instead of the contents of the stream. The actual output below:</p>
<pre><code>{&quot;parallel&quot; : false}
</code></pre>
<p>How can I instruct Jackson to serialize the content and not the stream object?</p>