I'm using Jersey-2.19.x in my application to call rest APIs. Since jersey clients are expensive to create, I wanted to reuse the client for my API calls and therefore inject the client via guice injection.
Here is my guice code for injecting jersey client:
@Provides
@Singleton
Client getClient() {
return ClientBuilder.newClient();
}
And here is my class that uses this injection:
@Inject
public MyClass(Client client) {
this.client = client;
}
public callPost(String uri) {
// code to set web target and invoke
}
Now my question is, how can I close this injected client? What happens if jersey clients are never closed? And what are some best practices for using jersey client via dependency injection?