11

when I have a method in Jersey that listens to a POST request, and when I have an InputStream as an argument in the method from which I want to get the data, do I need to close this InputStream or will Jersey handle this?

I haven't found any info on it. I had read RESTful Java with JAX-RS 2.0 a while back, and I don't remember if it was mentioned. Skimming through it now, I found several code examples, and the stream is not closed. I would think it's not necessary, but wanted to ask.

nomve
  • 736
  • 4
  • 14

2 Answers2

0

How do you get that InputStream reference? Through a MessageBodyReader? In that case if you are using a MessageBodyReader than you must not close the InputStream. There is even an explicit warning for this in the documentation.

7.2.2.2. MessageBodyReader.readFrom

Anyway this link applies to Jersey v2. I don't know what's about version 1. Which version do you use?

Alma Alma
  • 1,641
  • 2
  • 16
  • 19
  • Sorry for the late reply. It's not in the MessageBodyReader, I already know not to close those or the output in MessageBodyWriter for that matter. It's the InputStream you get when doing a POST request. The method argument not annotated with something like `@QueryParam` or `@PathParam` is then the InputStream or the data that is sent to the server from the client. – nomve Jul 13 '14 at 12:09
0

I had the same question but the current answer does not answer the question for the case when not using the MessageBodyReader. For those who do wonder there are two other answers here on SO (one, two)

The short answer and summary is to call close() on the InputStream.

Background:

  • one user debugged it and the stream is not automatically closed
  • the close() method is idempotent so a 2nd call wouldn't hurt
  • most of the users said to close() it or to use closeable with try-with-resources-statements
Lonzak
  • 9,334
  • 5
  • 57
  • 88