Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Closing JAX-RS StreamingOutput's OutputStream

Should the StreamingOutput's OutputStream be closed by the implementing class?

The java-doc does not give any recommendations. I guess it just delegates to the underlying ServletOutputStream which means it should not be closed, but my guess might be wrong :) Also the javadoc makes a reference to the MessageBodyWriter interface where it's explicitly said that the output stream must not be closed.

https://docs.oracle.com/javaee/7/api/javax/ws/rs/core/StreamingOutput.html

like image 358
Svetlin Zarev Avatar asked Sep 19 '16 12:09

Svetlin Zarev


People also ask

What is StreamingOutput in Java?

Interface StreamingOutput A type that may be used as a resource method return value or as the entity in a Response when the application wishes to stream the output. This is a lightweight alternative to a MessageBodyWriter .

What can a JAX-RS method return?

If the URI path template variable cannot be cast to the specified type, the JAX-RS runtime returns an HTTP 400 (“Bad Request”) error to the client. If the @PathParam annotation cannot be cast to the specified type, the JAX-RS runtime returns an HTTP 404 (“Not Found”) error to the client.


1 Answers

Being unable to find a definitive answer I took a look at jersey's source being the reference implementation. In Jersey the StreamingOutput is processed by a MessageBodyWriter by simply calling streamingOutput.write(os) [1] where os is the OutputStream passed to the MessageBodyWriter. That's good because its javadoc is quite clear that this OutputStream must not be closed.

To summarize: The OutputStream passed to the StreamingOutput must not be closed.

[1] https://github.com/jersey/jersey/blob/master/core-common/src/main/java/org/glassfish/jersey/message/internal/StreamingOutputProvider.java

like image 57
Svetlin Zarev Avatar answered Oct 01 '22 11:10

Svetlin Zarev