Search code examples
javarestclientput

Making multiple rest client calls


I'm trying to make multiple client calls using org.apache.wink.client.RestClient to a service with Basic Auth.

The first PUT i get status code of 200 but then i get a connection timed out.

I tried re-creating the client each time i'm going to make the new put.

Here's my code:

for (int pos = 0; pos < jsonsToSend.size(); pos++) {
        BasicAuthSecurityHandler secHandler = new BasicAuthSecurityHandler();
        ClientConfig config = new ClientConfig();
        secHandler.setUserName(username);
        secHandler.setPassword(password);
        config.handlers(secHandler);

        RestClient client = new RestClient(config);
        Resource resource = client.resource(url);

        actualJson = JSONObject.parse(jsonsToSend.get(pos).toString());
        response = putJson(actualJson, resource);
    }

This is the putJson method:

private ClientResponse putJson(JSONObject jsonSend, Resource resource) throws Exception {
    try {
        ClientResponse response = resource.contentType("text/plain; charset=utf-8").accept("*/*").put(jsonSend.toString());
        return response;
    } catch (Exception e) {
        throw e;
    }
}

Do you know how to make multiple rest calls?


Solution

  • Yes you could. Here is how I did: Service Layer: ... //First one ResponseEntity> firstResponse = firstClient.createEntity(requestPayload1);

    //Second one ResponseEntity> secondResponse = secondClient.createEntity(requestPayload2);

    Both could pass same requestPayload object.

    So, you need to have two clients (if you don't want to change the builder each time dynamically)

    I assume you know how to create the client already. In my case, I used restTemplate.getUriTemplateHandler() to create the URI, then RequestEntity and ParameterizedTypeReference to exchange.

    If you are not doing any transactional work, I would think calling in order as I did would be OK.