Accessing complex web services with Railo

Posted by Mark Drew on code on November 14, 2011

Tagged under

Edit: Now with screencast! see the bottom of the post

There are a number of times when a webservice returns some very complicated data, or inversely it requires some even more complicated parameters to be sent. In the past I have spent hours trying to figure out how to send the right parameters to remote method calls, but instead of wasting a long time using either Adobe ColdFusion's or Railo Server's Axis adaptors, I go straight to the source, and in this post, I shall show you how to do it. 

To start off, I am just going to use a simple web service I have created, but you can do this for even more complex services and get an idea what they return. 

This is the webservice I am going to be calling:

<cfreturn Now()>

Ok, not the most complicated, but I just want to use it so that we all know how to do the process. 

Next, I use a tool that comes with axis called WSDL2Java (both ACF and Railo use this in the background when making webservice calls. In CF they are called stubs), to do this I create a Java Project in Eclipse.

Then I create a lib folder, into which I copy ALL the .jar files that come with Railo Server (all the files in , for example /Railo-Express/lib/ext/). Now you won't need ALL of them but it's easier than trying to find out which ones you need. 

Next, by right clicking on your project > Properties > Java Build Path > Libraries I add all the .jar files that I copied over and click OK 

Awesome, now we are ready to use the WSDL2Java function, create a new run configuration under the "Java Application" section , give it a name and then set the main class to be "org.apache.axis.wsdl.WSDL2Java"

Then in the arguments section, add the WSDL url (in my case http://dev.local:8888/RemoteService.cfc?wsdl):

Now you can click "Apply" and then "Run"

If you now click Refresh on your project you will see a new folder has been added to the root: 

We can now move that folder to the "src" folder in our project to get it to compile:

Now for the real "Java Bit". I tend to write it first in Java to test the service, so create a new class in the src folder of your project, I called in Runner and make sure you tick the create "public static void main(String[] args)" checkbox, you should get some code like:

public class Runner \{

/**
 * @param args
 */
public static void main(String[] args) \{
	// TODO Auto-generated method stub

} } </code>

Great, now let's use the ServiceLocator to get our webservice and call methods on it:

public static void main(String[] args) throws RemoteException, ServiceException \{ Remoteservice_wrapServiceLocator ws = new Remoteservice_wrapServiceLocator(); String timeItem = ws.getRemoteServiceCfc().getTime(); System.out.println(timeItem); }

If we run this now we get the time returned from the webservice! But we are not finished yet. You want to use these stubs from your CFML code right?

First thing to do is wrap all this up as a JAR file, luckily this is pretty easy, just right click on the project and select Export... > Java > JAR file, and then select where you want to save it, I am saving it into my /Railo-Express/lib/ as a file called DemoWebService.jar.

Now to restart Railo so that the new jar gets picked up. 

Ok, let's use this in Railo, in this example, I just used the CreateObject() function with the third param that defines which jar to use:

ws = CreateObject("java", "local.dev.RemoteService_cfc.Remoteservice_wrapServiceLocator", "/Applications/railo-express/lib/DemoWebService.jar"); timeItem = ws.getRemoteServiceCfc().getTime(); WriteOutput(timeItem);

</code>

And there you go, you have called the method easily.

This is a simple example, but if you can do it from Java you can do it from CFML. If there are complex objects to pass, you can just create an instance of that object and pass it to the webservice too. 

Edit: I have now also created some screencasts so you can see how it works

Part One

Part Two

Part Three




comments powered by Disqus