Home Ask Login Register

Developers Planet

Your answer is one click away!

dev ツ February 2016

Closing connection in GET request using Jersey Client 2.22.1

I am using Jersey client for REST calls from Java code:


In my GET request,

javax.ws.rs.client.Invocation.Builder builder = ClientBuilder.newClient().target(url).request(); 

the client will be closed automatically after calling readEntity(String.class).

If I use,


I get the same output.

Is the connection closed automatically or do I need to close it manually in this case?


Cássio Mazzochi Molin February 2016

Short answer

For the following code:

String result = ClientBuilder.newClient().target(url).request().get(String.class);

Under the hood, Jersey invokes Response#readEntity(Class<T>) if the request has succeeded and the connection will be closed for you. So, the connection doesn't need to be closed manually in that situation.

For the following code:

Response response = ClientBuilder.newClient().target(url).request().get();

You need to invoke Response#close() to close the connection (or invoke Response#readEntity(Class<T>) to make Jersey close the connection for you).

Long answer

As stated in the documentation, if you don't read the entity, then you need to close the response manually by invoking response.close().

For more details, have a look at Jersey's documentation about how to close connections:

5.7. Closing connections

Post Status

Asked in February 2016
Viewed 2,924 times
Voted 5
Answered 1 times


Leave an answer

Quote of the day: live life