Search code examples
jax-rsjava-streamresteasyjava-ee-8

Is it possible to return Stream<?> from JAX-RS 2.1 resource?


Googling surprisingly did not show results! So, please, I am wondering if it is possible...: 1. as for any JAX-RS implementation (e.g. by spec) - did not find that one in the spec itself. 2. as for some particular one (like a provider specific capability, I am currently on RestEasy btw) 3. and if yes, should I close the stream of, say, it came from hibernate scrollable result e.g. Session#stream()? Or does JAX-RS closes automatically or at least because as I remember all Collections etc. are represented as Lists in JAX-RS?

I believe I saw it somewhere in 2.1 presentation on SlideShare.


Solution

  • Yes, it is supported as of JAX-RS 2.1 for sure.

    For serializing response bodies a SNAPSHOT of "currently-in-development" jackson-jaxrs-X-provider can be used which at the time of writing this post could only be locally built with maven from relevant FasterXML repository. All other needed components except just jackson-jaxrs-X-provider of Jackson 3.x (like jackson core, jackson-annotations, etc.) are available in OSS SNAPSHOT repo.

    https://oss.sonatype.org/content/repositories/snapshots/com/fasterxml/

    Tried it, it works fine.