groovyws-client

Groovy: WSClient throwing JAXBException


I am trying to call a simple public Web Service with WSClient in a Groovy script, but it explodes when initializing ...

TestService.groovy:

@Grab(group='org.codehaus.groovy.modules', module='groovyws', version='0.5.2')
import groovyx.net.ws.WSClient

def proxy = new WSClient("http://www.w3schools.com/webservices/tempconvert.asmx?WSDL", this.class.classLoader)
proxy.initialize();

def result = proxy.CelsiusToFahrenheit(0)
println "You are probably freezing at ${result} degrees Farhenheit"

The error message:

SEVERE: Could not compile java files for http://www.w3schools.com/webservices/tempconvert.asmx?WSDL.
Caught: java.lang.IllegalStateException: Unable to create JAXBContext for generated packages: Provider com.sun.xml.bind.v2.ContextFactory could not be instantiated: javax.xml.bind.JAXBException: "org.tempuri" doesnt contain ObjectFactory.class or jaxb.index
java.lang.IllegalStateException: Unable to create JAXBContext for generated pack
ages: Provider com.sun.xml.bind.v2.ContextFactory could not be instantiated: jav
ax.xml.bind.JAXBException: "org.tempuri" doesnt contain ObjectFactory.class or j
axb.index
        at org.apache.cxf.endpoint.dynamic.DynamicClientFactory.createClient(DynamicClientFactory.java:343)
        at org.apache.cxf.endpoint.dynamic.DynamicClientFactory.createClient(DynamicClientFactory.java:196)
        at org.apache.cxf.endpoint.dynamic.DynamicClientFactory.createClient(DynamicClientFactory.java:175)
        at groovyx.net.ws.AbstractCXFWSClient.createClient(AbstractCXFWSClient.java:229)
        at groovyx.net.ws.WSClient.initialize(WSClient.java:108)
        at groovyx.net.ws.IWSClient$initialize.call(Unknown Source)
        at TestService.run(TestService.groovy:5)
Caused by: javax.xml.bind.JAXBException: Provider com.sun.xml.bind.v2.ContextFactory could not be instantiated: javax.xml.bind.JAXBException: "org.tempuri" doesnt contain ObjectFactory.class or jaxb.index - with linked exception:
[javax.xml.bind.JAXBException: "org.tempuri" doesnt contain ObjectFactory.classor jaxb.index]
        at org.apache.cxf.endpoint.dynamic.DynamicClientFactory.createClient(DynamicClientFactory.java:340)
        ... 6 more
Caused by: javax.xml.bind.JAXBException: "org.tempuri" doesnt contain ObjectFactory.class or jaxb.index
        at com.sun.xml.bind.v2.ContextFactory.createContext(ContextFactory.java:197)
        ... 7 more

Any hint? Why should I have a jaxb.index?

Just discovered that the problem occurs with Java 1.7 (jdk1.7.0_21)... it's OK when running with Java 6 (jdk1.6.0_31)

Any hint to work with Java 7?


Solution

  • As noted on the GroovyWS page, GroovyWS is currently dormant. You could do the same thing (albeit with a wordier syntax) using the groovy-wslite library:

    @Grab(group='com.github.groovy-wslite', module='groovy-wslite', version='0.8.0')
    import wslite.soap.*
    
    def client = new SOAPClient('http://www.w3schools.com/webservices/tempconvert.asmx')
    def response = client.send(SOAPAction:'http://tempuri.org/CelsiusToFahrenheit') {
        body {
            CelsiusToFahrenheit('xmlns':'http://tempuri.org/') {
                Celsius('0')
            }
        }
    }
    
    def result = response.CelsiusToFahrenheitResponse.CelsiusToFahrenheitResult.text()
    println "You are probably freezing at ${result} degrees Farhenheit"
    

    Note that this requires you to look at the WSDL to get the SOAP message namespace, unlike the GroovyWS version of the code. But it works!