Performing requests using a REST client 

We can send a request either synchronously or asynchronously. In synchronous mode, the calling thread is blocked until a response is received. In asynchronous mode, we provide a ResponseListener object with two callback methods, the onSuccess and onFailure methods, to be notified when the request completes. The declaration of the synchronous request method is shown in the following code snippet:

public Response performRequest​(Request request) throws java.io.IOException

The declaration of the asynchronous request method is shown in the following code snippet:

public void performRequestAsync​(Request request, ResponseListener responseListener)

Before we can send out the request, we have to construct a Request object. The following example is for a POST request with a uRL and a requestBody in a String type. The request is passed down to the performRequest method of our RestClient wrapper object, llRestClient, to perform the operation:

    Request request = new Request("POST", uRL);
request.setJsonEntity(requestBody);
Map<String,Object> response = llRestClient.performRequest(request);

The following code block describes the programming structure of the synchronous performRequest method. First of all, the restClient has been autowired to an instance created by the LowLevelRestClient() method. There is a try-catch block to intercept exceptions. For our own purposes, we use the convertValue() method from the ObjectMapper class to convert the response into a Map<String, Object> object:

public Map<String, Object> performRequest(Request request) {
RestClientResponse clientResponse = new RestClientResponse();
try {
response = restClient.performRequest(request);
clientResponse.setStatusCode(response.getStatusLine().getStatusCode());
clientResponse.setResponseBody(EntityUtils.toString(response.getEntity()));
clientResponse.setHeaders(response.getHeaders());
} catch (Exception ex) {
...
}
Map<String, Object> convertValue = (Map<String, Object>) (newObjectMapper())
.convertValue(
clientResponse, Map.class);
return convertValue;
}

The following code block describes the programming structure of the asynchronous performRequestAync method. We make the asynchronous method and the synchronous method return the same object. In our design, we can put the handling codes in the onSuccess() callback method in cases of success, and the error handling codes in onFailure() in case it should fail:

public Map<String, Object> performAsyncRequest(Request request) {
RestClientResponse clientResponse = new RestClientResponse();
restClient.performRequestAsync(request, new ResponseListener() {
@Override
public void onSuccess(Response response) {
...
}
@Override
public void onFailure(Exception exception) {
}
});
clientResponse.setStatusCode(200);
Map<String, Object> convertValue =v(Map<String, Object>)
(
new ObjectMapper()).convertValue(clientResponse, Map.class);
return convertValue;
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
52.14.174.133