2

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

测试服务.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 的提示?

4

1 回答 1

2

正如GroovyWS 页面上所述,GroovyWS 当前处于休眠状态。您可以使用groovy-wslite 库做同样的事情(尽管语法更冗长):

@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 版本的代码不同。但它有效!

于 2013-07-12T01:47:37.233 回答