javarestjava-ws

Need to create javax.ws.rs.client.Client for each webservcie call


Create a javax.ws.rs-ap.jar client and send the request to the server:

   javax.ws.rs.client.Client client = ClientBuilder.newBuilder().build();

I have writtten the above code to call multiple api(get user by id, get all users and deleteUser).

My question is here.

I am creating a new Client for each api cal.

Just wanted to know i can create a single instance of Client and make multiple calls ?


Solution

  • Yes, a Client can be reused as long as it's lifecycle is managed properly. This means when a client instance is created, it should be closed properly as well using the close() method, once it's purpose is served. Refer to the Client API documentation.

    Note that multiple client instances would be needed if each client has a different client configuration.