Do I have to use try-finally
to ensure that JAX-RS Response
is closed after getting the Response
?
Building and Invoking Requests | RESTful Java with JAX-RS 2.0 (Second Edition) uses try..finally:
Version 1
Response response = client.target("http://commerce.com/customers/123")
.accept("application/json")
.get();
try {
if (response.getStatus() == 200) {
response.bufferEntity();
Customer customer = response.readEntity(Customer.class);
Map rawJson = response.readEntity(Map.class);
}
} finally {
response.close();
}
I cannot use try-with-resource, as JAX-RS Response
does not implement AutoClosable
. So why do I have to use try-finally
?
Version 2 (without try-finally
)
if (response.getStatus() == 200) {
response.bufferEntity();
Customer customer = response.readEntity(Customer.class);
Map rawJson = response.readEntity(Map.class);
}
response.close();
My Question is
The response is also always closed with Version 2, am I right?