0

Apparently, Groovy easily consumes web services. Can it consume a web service that needs JAX-RPC instead of JAX-WS? Should I use an older version of Groovy or its libraries to do so?

3 Answers 3

2

It's really easy to consume XML-RPC web services. You need the Groovy XML-RPC as well as the Smack library in your classpath.

I wrote some groovy scripts to work with our Atlassian Confluence wiki and here's a short example to retrieve a wiki page using XML-RPC:

import groovy.net.xmlrpc.*

def c = new XMLRPCServerProxy("http://host:port/rpc/xmlrpc")
def token = c.confluence1.login("username","password")

def page = c.confluence1.getPage(token, "SPACE", "pagename")
println page.content

c.confluence1.logout(token);

You use the XMLRPCServerProxy to access the XML-RPC services. If your services require complex parameters as parameters or return one, these are represented as Groovy maps, with the attribute name as key and it's value as the corresponding value. In the script above, the service getPage returns a Page object, which is a map, but as you can directly access a map's key using the dot-notation in Groovy, page.content is the same as page.get("content").

2
  • Thanks Chris, this looks very cool but I'm probably a few chapters (books?) behind you. I never imagined an XMPP library would relate to XML-RPC until Google showed me Jabber-RPC. My head hurts already!
    – Paul
    Nov 4, 2009 at 1:58
  • Actually, I haven't cared much about the fact that the Groovy XMLRPC module has a dependency with an XMPP library. I just tried the example from groovy.codehaus.org/XMLRPC and it worked. Nov 4, 2009 at 10:26
0

What do you mean by "can it consume a web service that needs JAX-RPC instead of JAX-WS"? What differences do you expect on the Groovy side? Did you try to call that web service as documented:

import groovyx.net.ws.WSClient

def proxy = new WSClient("http://localhost:6980/MathService?wsdl", this.class.classLoader)
proxy.initialize() // from 0.5.0
def result = proxy.add(1.0 as double, 2.0 as double)
assert (result == 3.0)

result = proxy.square(3.0 as double)
assert (result == 9.0)

Do you get any particular error?

5
  • Hi, Pascal! I'm in NetBeans as you suggested. Used wizard to build WS client, and had to add the JAX-RPC library. Not knowing Java and not seeing (yet) how to invoke that client with my data, I saw how ws clients look easy in Groovy and that NetBeans would let me add a Groovy class or script. But when I added a simple Groovy script to my project, I don't know how to see the output, or even if it ran. When I start a new project, it wants me to install Grails & whatnot which I hope I don't need. Super-newbie, I know...
    – Paul
    Nov 3, 2009 at 1:41
  • Ho, hi Paul! Sorry, I didn't recognize you immediately :) Actually, I'm not sure why you had to add the JAX-RPC library. My initial suggestion was to implement a JAX-WS client (a bit simpler) and AFAIK Netbeans includes everything required for this. Maybe you should create another question for that, it would be easier to discuss your issues. Nov 3, 2009 at 23:31
  • I may need the JAX-RPC library by stumbling into a small example of why people seek alternatives to SOAP. Apparently there are multiple flavors of WDSL (search "ibm style wsdl" for the obscure details). I saw various temptingly simple examples of ws clients in Groovy, but despite hours of bumbling with Groovy console and/or IntelliJ community beta, none work yet :(. Maybe I'm trying to hard to be groovy.
    – Paul
    Nov 4, 2009 at 1:51
  • I think you are misinterpreting things like binding style (and use) and the "library" you'll need. Personally, I prefer to use document/literal style for better interoperability and the "wrapped" convention for an RPC-like programming experience (see atmanes.blogspot.com/2005/03/…). However, choosing rcp/literal doesn't mean that you have to use JAX-RPC and, in your case, I'd rather stick with JAX-WS (and Java) and use annotations to stay away from all these details. Nov 4, 2009 at 2:47
  • Very possibly. The NetBeans wizard saw the WSDL for my service and said I needed JAX-RPC. This monkey with a mouse didn't know enough to argue :-).
    – Paul
    Nov 4, 2009 at 18:33
0

Since Groovy can work with compiled Java classes, sometimes the easiest way to access a SOAP-based web service is to just generate the stubs and write a Groovy client that uses them. Use your "wsimport" tool (JAX-WS) or wsdl2java (JAX-RPC) to generate the stubs, and write your Groovy class as usual.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.