Thursday 1 April 2021

StreamingOutput : streaming large json responses in REST API

So I had to move a large number of database objects into a JSON response, and I was looking for a neat way to do it.

Originally, I already had the idea of having the database generate JSON responses for me. You can find more information on this here1.

But then I thought it would make sense to, instead of loading all those json strings from the database in memory, streaming them to the JSON generator in my REST Service.

It looked quite easy.

Database streaming

Now, I found out that instead of getResultList(), I could use getResultStream(). Unfortunately, the default implementation of getResultStream() just delegates to getResultList(), which doesn't help at all2.

Also, there seems to be a number of other pitfalls to fall into as well2, 3. So, my first attempt displayed here just might be flawed, or might be flawed if you're using something other than MariaDB.

My attempt at a scrolling cursor, instead of just dumping the entire thing in our lap.

Streaming as a REST Service

So I found some information on how to do this at [4].

@GET
@Produces(
{
  "application/json"
})
public Response findAll(@Context UriInfo info)
{
  StreamingOutput stream = StreamerHelper.getStream(getEntityManager(), AdminItem.GET_QUERY);   return Response.ok(stream).build();
}

I think I could use the Fetch way5 on the client side, but in my case it's not needed.

References

[1] Using SQL to generate JSON output
http://randomthoughtsonjavaprogramming.blogspot.com/2020/01/using-sql-to-generate-json-output.html
[2] JPA 2.2’s new getResultStream() method and how you should NOT use it
https://thorben-janssen.com/jpa-2-2s-new-stream-method-and-how-you-should-not-use-it/
[3] Vlad Mihalcea - Hibernate performance tuning tips
https://vladmihalcea.com/hibernate-performance-tuning-tips/
[4] Response streaming between JAX-RS and Web-Components (Part 1)
https://schoeffm.github.io/posts/response-streaming-between-jaxrs-and-webcomponents-part1/
[5] Response streaming between JAX-RS and Web-Components (Part 2)
https://schoeffm.github.io/posts/response-streaming-between-jaxrs-and-webcomponents-part2/

No comments:

Post a Comment