Short answer
Consider the following code:
Client client = ClientBuilder.newClient(); String result = client.target(url).request().get(String.class);
Under the hood, Jersey calls Response#readEntity(Class<T>) if the request succeeds and the connection is closed to you. Therefore, in this situation, the connection does not need to be closed manually.
Now consider the following code:
Client client = ClientBuilder.newClient(); Response response = client.target(url).request().get();
In this situation, you need to call Response#close() to close the connection. Or call Response#readEntity(Class<T>) to force Jersey to close the connection for you.
Long answer
As stated in the documentation , if you do not read the object , then you need to close the response manually by calling Response#close() .
For more information, see the documentation on how to close connections:
5.7. Closing Connections
Suitable connections are opened for each request and closed after receiving a response and processing the object (read object). See the following example:
final WebTarget target = ... some web target Response response = target.path("resource").request().get(); System.out.println("Connection is still open."); System.out.println("string response: " + response.readEntity(String.class)); System.out.println("Now the connection is closed.");
If you are not reading the object, you need to close the response manually response.close() .
Also, if an object is read in an InputStream (via response.readEntity(InputStream.class) ), the connection remains open until you finish reading with InputStream . In this case, the InputStream or Response should be manually closed at the end of reading from the InputStream .
Also, see the JerseyInvocation source . The following are the most important parts.
In the translate(ClientResponse, RequestScope, Class<T>) method translate(ClientResponse, RequestScope, Class<T>) you will see that response.readEntity(Class<T>) called.
Call the HTTP GET method for the current request synchronously.
@Override public <T> T get(final Class<T> responseType) throws ProcessingException, WebApplicationException { return method("GET", responseType); }
Call an arbitrary method for the current request synchronously.
@Override public <T> T method(final String name, final Class<T> responseType) throws ProcessingException, WebApplicationException {
Synchronously call a request and receive a response of the specified type back.
@Override public <T> T invoke(final Class<T> responseType) throws ProcessingException, WebApplicationException {
JerseyInvocation#translate(ClientResponse, RequestScope, Class<T>)
If the request Response#readEntity(Class<T>) , the response object is read as an instance of the specified Java type using Response#readEntity(Class<T>) :
private <T> T translate(final ClientResponse response, final RequestScope scope, final Class<T> responseType) throws ProcessingException { if (responseType == Response.class) { return responseType.cast(new InboundJaxrsResponse(response, scope)); } if (response.getStatusInfo().getFamily() == Response.Status.Family.SUCCESSFUL) { try { return response.readEntity(responseType); } catch (final ProcessingException ex) {