jax rs - StreamingOutput in jax-rs usecases? -


jax-rs provides streamingoutput interface can implement raw streaming of our response bodies.

public interface streamingoutput {     void write(outputstream output) } 

i not sure why go building interface expose response outputstream. why not inject outputstream directly , can write on it!!

in book restful java jax-rs 2.0 written bill burk (one of resteasy authors), find explanation streamingoutput.

the same question asked answered author:

streamingoutput simple callback interface implement when want raw streaming of response bodies [...]

you allocate implemented instances of interface , return them jax-rs resource methods. when jax-rs runtime ready write response body of message, write() method invoked on streamingoutput instance. [...]

you may asking yourself, “why not inject outputstream directly? why have callback object streaming output?” that’s question! reason having callback object gives jax-rs implementation freedom handle output wants. performance reasons, may beneficial jax-rs implementation use different thread other calling thread output responses. more importantly, many jax-rs implementations have interceptor model abstracts things out automatic gzip encoding or response caching. streaming directly can bypass these architectural constructs. finally, servlet 3.0 specification has introduced idea of asynchronous responses. callback model fits in nicely idea of asynchronous http within servlet 3.0 specification.

and streamingoutput documentation states following:

this lightweight alternative messagebodywriter.


Comments

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -