有人让我知道如何通过 DynamicClientFactory 使用绑定文件创建客户端对象。
这是我测试过的示例代码。它发生了异常。
String wsdlURL = "http://localhost/test?wsdl";
ClassLoader loader = Thread.currentThread().getContextClassLoader();
JaxWsDynamicClientFactory factory = JaxWsDynamicClientFactory.newInstance();
List<String> bindingFiles = new ArrayList<String>();
File bindingFile = new File("custom.xml");
try {
bindingFiles.add(bindingFile.getCanonicalPath().toString());
} catch (IoException e) {
e.printStackTrace();
}
Client client = factory.createClient(wsdlURL, loader, bindingFiles);
下面是custom.xml。
<bindings xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
wsdlLocation="http://localhost/test?wsdl"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
jaxb:version="2.0" jaxb:extensionBindingPrefixes="xjc"
xmlns="http://java.sun.com/xml/ns/jaxws">
<enableWrapperStyle>true</enableWrapperStyle>
<jaxb:globalBindings>
<xjc:simple/>
</jaxb:globalBindings>
</bindings>
当我在示例代码之上运行时,我得到了以下异常..
java.lang.reflect.UndeclaredThrowableException
at $Proxy18.bind(Unknown Source)
at org.apache.cxf.endpoint.dynamic.DynamicClientFactory.createClient(DynamicClientFactory.java:318)
at org.apache.cxf.endpoint.dynamic.DynamicClientFactory.createClient(DynamicClientFactory.java:252)
at DynamicWebServiceClient_test.test(DynamicWebServiceClient_test.java:93)
at DynamicWebServiceClient_test.main(DynamicWebServiceClient_test.java:54)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.cxf.common.util.ReflectionInvokationHandler.invoke(ReflectionInvokationHandler.java:52)
... 5 more
Caused by: java.lang.reflect.UndeclaredThrowableException
at $Proxy20.fatalError(Unknown Source)
at com.sun.tools.internal.xjc.api.impl.s2j.SchemaCompilerImpl.fatalError(SchemaCompilerImpl.java:279)
at com.sun.tools.internal.xjc.util.ErrorReceiverFilter.fatalError(ErrorReceiverFilter.java:71)
at com.sun.xml.internal.xsom.impl.parser.ParserContext$2.fatalError(ParserContext.java:196)
at com.sun.xml.internal.xsom.impl.parser.NGCCRuntimeEx.unexpectedX(NGCCRuntimeEx.java:484)
at com.sun.xml.internal.xsom.impl.parser.state.NGCCHandler.unexpectedEnterElement(NGCCHandler.java:179)
at com.sun.xml.internal.xsom.impl.parser.state.Schema.enterElement(Schema.java:401)
at com.sun.xml.internal.xsom.impl.parser.state.NGCCRuntime.startElement(NGCCRuntime.java:243)
at org.xml.sax.helpers.XMLFilterImpl.startElement(XMLFilterImpl.java:527)
at com.sun.tools.internal.xjc.util.SubtreeCutter.startElement(SubtreeCutter.java:93)
at com.sun.tools.internal.xjc.reader.ExtensionBindingChecker.startElement(ExtensionBindingChecker.java:133)
at org.xml.sax.helpers.XMLFilterImpl.startElement(XMLFilterImpl.java:527)
at com.sun.tools.internal.xjc.reader.xmlschema.parser.IncorrectNamespaceURIChecker.startElement(IncorrectNamespaceURIChecker.java:102)
at org.xml.sax.helpers.XMLFilterImpl.startElement(XMLFilterImpl.java:527)
at com.sun.tools.internal.xjc.reader.xmlschema.parser.CustomizationContextChecker.startElement(CustomizationContextChecker.java:177)
at org.xml.sax.helpers.XMLFilterImpl.startElement(XMLFilterImpl.java:527)
at com.sun.tools.internal.xjc.reader.internalizer.DOMForestScanner$LocationResolver.startElement(DOMForestScanner.java:130)
at com.sun.xml.internal.bind.unmarshaller.DOMScanner.visit(DOMScanner.java:228)
at com.sun.xml.internal.bind.unmarshaller.DOMScanner.scan(DOMScanner.java:111)
at com.sun.tools.internal.xjc.reader.internalizer.DOMForestScanner.scan(DOMForestScanner.java:78)
at com.sun.tools.internal.xjc.reader.internalizer.DOMForestScanner.scan(DOMForestScanner.java:86)
at com.sun.tools.internal.xjc.reader.internalizer.DOMForestParser.parse(DOMForestParser.java:88)
at com.sun.tools.internal.xjc.ModelLoader$XMLSchemaParser.parse(ModelLoader.java:251)
at com.sun.xml.internal.xsom.impl.parser.NGCCRuntimeEx.parseEntity(NGCCRuntimeEx.java:326)
at com.sun.xml.internal.xsom.impl.parser.ParserContext.parse(ParserContext.java:113)
at com.sun.xml.internal.xsom.parser.XSOMParser.parse(XSOMParser.java:154)
at com.sun.xml.internal.xsom.parser.XSOMParser.parse(XSOMParser.java:143)
at com.sun.tools.internal.xjc.ModelLoader.createXSOM(ModelLoader.java:505)
at com.sun.tools.internal.xjc.api.impl.s2j.SchemaCompilerImpl.bind(SchemaCompilerImpl.java:226)
... 10 more
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.cxf.common.util.ReflectionInvokationHandler.invoke(ReflectionInvokationHandler.java:52)
... 39 more
Caused by: java.lang.RuntimeException: Fatal error compiling schema from WSDL at {http://localhost/test?wsdl}: Unexpected <jaxws:bindings> appears at line 18 column 62
at org.apache.cxf.endpoint.dynamic.DynamicClientFactory$InnerErrorListener.fatalError(DynamicClientFactory.java:739)
... 44 more
Caused by: org.xml.sax.SAXParseException: Unexpected <jaxws:bindings> appears at line 18 column 62
at com.sun.xml.internal.xsom.impl.parser.NGCCRuntimeEx.unexpectedX(NGCCRuntimeEx.java:477)
... 34 more