Groovy:WSClient抛出JAXBException

时间:2013-07-08 21:12:35

标签: groovy ws-client

我试图在Groovy脚本中使用WSClient调用一个简单的公共Web服务,但在初始化时它会爆炸......

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"

错误消息:

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

任何提示?我为什么要有jaxb.in​​dex?

刚刚发现Java 1.7(jdk1.7.0_21)出现问题...使用Java 6(jdk1.6.0_31)运行时没关系

任何使用Java 7的提示?

1 个答案:

答案 0 :(得分:2)

the GroovyWS page所述,GroovyWS目前处于休眠状态。您可以使用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"

请注意,这需要您查看WSDL以获取SOAP消息名称空间,这与GroovyWS版本的代码不同。但它有效!