Connecting to an API WSDL through Java REST

Description

Java seems to have a pretty decent library for dealing with WSDL soap transactions, however there doesn’t seem to be any great library out there for dealing with rest. The solution is to using java’s built httpconnection library to post the information to the appropriate url and wait for a response. For REST protocol we will first encode a JSON array that contains the proper commands and parameters according the WSDL API you are trying to interface with. Next the JSON encoded array is posted to a url, which should return a response that can be processed.

Encoding in JSON

The first thing to do is look at the API’s web service description language to see the format of the JSON encoded array should be made. The following is an example from the InsideSales.com WSDL. This method call is meant to delete a lead from the system.
deleteLead
[java]
public int deleteLead(int leadId, boolean purge) {
int returnLeadId = 0;
JSONObject json = new JSONObject();
JSONArray list = new JSONArray();
try {
list.put(leadId);
list.put(purge);
json.put("operation", "deleteLead");
json.put("parameters", list);
} catch (JSONException e) {
e.printStackTrace();
}
String response = wsm.sendJson(json);
returnLeadId = Integer.valueOf(response);
return returnLeadId;
}

[/java]

The method wsm.sendJson(json) is in the next section. I made a wrapper class to handle various api connections and responses. I put these all in a class I called WebServiceManager.

HTML Post

Once we have the JSON encoded array we need to post it to the appropriate URL. Any WSDL documentation should give the url that the JSON object should be posted to. This means we need to have a function that will allow us to use Java to make the post.
[java]
public String sendJson(JSONObject json) {
HttpResponse response = null;
httpClient = HttpClientBuilder.create().build();
StringBuilder sb = new StringBuilder();
try {
request = new HttpPost(destination);
StringEntity params = new StringEntity(json.toString());
request.addHeader("content-type", "application/x-www-form-urlencoded");
request.addHeader("Cookie", this.cookies);
request.setEntity(params);
response = httpClient.execute(request);
InputStream ips = response.getEntity().getContent();
BufferedReader buf = new BufferedReader(new InputStreamReader(ips,"UTF-8"));
if(response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
throw new Exception(response.getStatusLine().getReasonPhrase());
}
String s;
while((s = buf.readLine()) != null) {
sb.append(s);
}
buf.close();
ips.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}

[/java]
http map

The image to the right shows the basic idea of how the hypertext transfer protocol works with a post request. Almost all API’s that exist will require some type of authentication, which I don’t show here, but you do some type of a login method and the server will send back a session id or some type of a cookie that you can store and use for subsequent methods so you don’t need to authenticate with every API request. This is why we send the cookie that we got from authentication with every request. The actual JSON encoded array of commands and parameters goes in the entity body of the post. After the server responds saying that the request is good we can read the response from the server.


Decoding JSON

In the previous section we send an http post request, and then read the response stream. The method deleteLead in this case returns the id of the lead that was deleted, so I just parse the string into an integer and handle it as I need. Other methods such as getLead will return a whole lead object, and in this section the JSON encoded response array will need to be decoded and put into a lead object.
[java]
String response = wsm.sendJson(json);
returnLeadId = Integer.valueOf(response);
return returnLeadId;
[/java]

Leave a Reply

Your email address will not be published. Required fields are marked *