Thursday, May 21, 2009

Complex elements in Groovy WS

Update, moved to Just Thinking: Code

I've been trying to use GroovyWS to test Web Services we are creating. Because of the way we coded the Web Services, I could not use the examples that are online, even the ones that are labeled "Complex objects".

If I used :
object.value = "value"
I would get an exception about not being able to convert the value to a javax.xml.bind.JAXBElement

I finally got to a point that I really needed to use some type of scripting language to access the Web Services, or create an entire application in Java to do it. I decided to take a good look at why I could use Groovy. Here is an example as to how I got it to work. It may not be very Groovy, so any suggestions as to how to make it more Groovy are appreciated. I hope this helps someone.

import groovyx.net.ws.WSClient

def getProxy(wsdl, classLoader) {
new WSClient(wsdl, classLoader)
}

def element(tagName, value) {
new javax.xml.bind.JAXBElement(new javax.xml.namespace.QName(tagName), value.class, value)
}

proxy = getProxy('http://localhost/ServicePort?WSDL', this.class.classLoader)
proxy.initialize()

def sampleData = proxy.create("com.sample.application.Sample")

sampleData.name = element("Name", "Item One")
sampleData.type = element("Type", "Misc");

result = proxy.store(sampleData)

println result.

No comments: