Post

Consuming SOAP in Groovy

I’ve started using SOAP in Groovy. As I’m new both to SOAP and to Groovy , this is a little basic.

Note: all of the following is checked for Windows 7, jdk 1.6.0_21. , groovyws 0.5.2, eclipse 3.6 (Helios), groovy 1.7.5

**What is SOAP? **
You can find the w3c tutorial here , but you don’t need me for that.
Basically , it solves the problem of”How the hell should I know what your web service expects me to send him?!”

It’s done in the following way: when someone publishes a SOAP web service , you publish a url that looks like”http://…..wsdl” . (like this: http://soapclient.com/xml/soapresponder.wsdl). This url is actually an xml document , that has pre-defiend nodes , that tell you what are the names of the function , what they expect to get , and what they return.

The cool thing is that most SOAP libraries know to do everything by themselves : You just give them the wsdl address , and they already create all the objects for you , and absolve you from the annoyance of parsing the xml and building objects you really couldn’t care less about.

More hands on:
First of all , a very simple & basic soap service to use when you’re writing your SOAP version”Hello, World” : http://soapclient.com/xml/soapresponder.wsdl (Don’t see anything? You’re probably using Chrome , aren’t you? Chrome has an issue with XML)

It has one method , called Method1(String a, String b) and return the 2 strings.

    import groovyx.net.ws.WSClient

    def proxy = new WSClient('http://soapclient.com/xml/soapresponder.wsdl', this.class.classLoader)
    proxy.initialize();
    println proxy.Method1("Hello,","World");

So far , everything looks nice. But for a lot of people trying to run this , it won’t work. Why?
Because of countless dependency issues.

  1. You need to get the groovyws-0.5.2.jar (or later) for the WSClient. (It won’t compile otherwise)
  2. Now it compiles , but it throws nasty errors your way , ahh? You’re probably getting this error :
"Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/cxf/endpoint/Client

It’s because what you have is only an API , and you need countless other dependencies.

  1. Hopefully , you’re working on a maven project (aren’t you? see next comment) . Add this dependency:
<dependency>
    <groupId>org.codehaus.groovy.modules</groupId>
    <artifactId>groovyws</artifactId>
    <version>0.5.2</version>
</dependency>
  1. Build your project a new . maven will take care off all the dependencies for you.
  2. Still doesn’t work? you get this nasty error? add this dependency to the pom:
<dependency>
    <groupId>xerces</groupId>
    <artifactId>xercesImpl</artifactId>
    <version>2.8.1</version>
</dependency>

Now you can finally run!

**Don’t use a maven project? **
You have 2 options:

  1. Somewhere in the WWW there is a groovyws-standalone-0.5.2.jar , which includes all the
    dependencies. I , unfortunately , couldn’t find it anywhere.
  2. Make a pom.xml as mentioned above , with the dependencies , and build it . All the
    dependencies will be in your repository , and you can use them from there.
This post is licensed under CC BY 4.0 by the author.

Comments powered by Disqus.