我刚刚尝试从WSDL文件(使用XFire和XMLBeans绑定)生成Java客户端代码。
我成功生成了客户端和故障消息(没有错误),但是输入消息和输出消息没有生成,而且在客户端中也没有生成操作。我的WSDL文件有问题吗?还是我遗漏了什么?
更新:
我更新了我的测试XFire项目这里。
我开始怀疑问题可能与WSDL有关(因为我可以成功生成其他WSDL)。我发现了以下警告,我认为它们相关:
WS-I:(BP2402)wsdl:binding元素未使用 soapbind:binding元素,如WSDL 1.1规范的“3 SOAP Binding”中所定义。
WS-I:(BP2032)有缺陷的soapbind:fault元素: “name”属性值与父元素wsdl:fault上的“name”属性值不匹配。
WS-I:(AP2901)描述在wsdl:input或wsdl:output元素的wsdl:binding上既不使用WSDL MIME Binding(如WSDL 1.1第5节所述),也不使用WSDL SOAP binding(如WSDL 1.1第3节所述)。
刚刚发现soap12可能会导致问题。如果我将xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap12/"更改为xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"并删除soap:operation中的soapActionRequired,就可以成功生成客户端。但是该Web服务目前仅在soap1.2中。因此,在这里更改wsdl以使用soap1.1不适用。
这是我的WSDL文件:
<!--Created by TIBCO WSDL-->
<wsdl:definitions xmlns:tns="http://schemas.ocbc.com/soa/WSDL/service/CBS-CustAccountInfo-I" xmlns:soap1="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:jndi="http://www.tibco.com/namespaces/ws/2004/soap/apis/jndi" xmlns:ns="http://schemas.ocbc.com/soa/emf/common/envelope/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:jms="http://www.tibco.com/namespaces/ws/2004/soap/binding/JMS" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" name="Untitled" targetNamespace="http://schemas.ocbc.com/soa/WSDL/service/CBS-CustAccountInfo-I">
<wsdl:types>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://schemas.ocbc.com/soa/emf/common/envelope/" elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:include schemaLocation="../Schemas/XML/CBS-CustAccountInfo-I-ServiceEnvelope.xsd"/>
</xs:schema>
</wsdl:types>
<wsdl:service name="CBS-CustAccountInfo-I">
<wsdl:port name="CBS-CustAccountInfo-I_HTTP" binding="tns:CBS-CustAccountInfo-I_HTTPBinding">
<soap:address location="https://localhost:15038/Services/CBS-CustAccountInfo-I/Processes/MainRequestResponse_HTTP"/>
</wsdl:port>
</wsdl:service>
<wsdl:portType name="PortType">
<wsdl:operation name="CBS-CustAccountInfo-I">
<wsdl:input message="tns:InputMessage"/>
<wsdl:output message="tns:OutputMessage"/>
<wsdl:fault name="fault1" message="tns:FaultMessage"/>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="CBS-CustAccountInfo-I_HTTPBinding" type="tns:PortType">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="CBS-CustAccountInfo-I">
<soap:operation style="document" soapAction="/Services/CBS-CustAccountInfo-I/Processes/MainRequestResponse_HTTP" soapActionRequired="true"/>
<wsdl:input>
<soap:body use="literal" parts="InputMessage"/>
</wsdl:input>
<wsdl:output>
<soap:body use="literal" parts="OutputMessage"/>
</wsdl:output>
<wsdl:fault name="fault1">
<soap:fault use="literal" name="fault1"/>
</wsdl:fault>
</wsdl:operation>
</wsdl:binding>
<wsdl:message name="InputMessage">
<wsdl:part name="InputMessage" element="ns:ServiceEnvelope"/>
</wsdl:message>
<wsdl:message name="OutputMessage">
<wsdl:part name="OutputMessage" element="ns:ServiceEnvelope"/>
</wsdl:message>
<wsdl:message name="FaultMessage">
<wsdl:part name="FaultMessage" element="ns:ServiceEnvelope"/>
</wsdl:message>
</wsdl:definitions>
以下是我创建Ant任务的代码:
<!-- Generating XML Beans -->
<target name="gen-xmlbeans">
<java classname="org.apache.xmlbeans.impl.tool.SchemaCompiler"
classpathref="build.classpath"
fork="true">
<arg value="-out"/>
<arg value="${basedir}/lib/ocbc.jar"/>
<arg value="${schema.path}"/>
</java>
</target>
<!-- Generating Client -->
<target name="ws-generate">
<taskdef name="wsgen" classname="org.codehaus.xfire.gen.WsGenTask">
<classpath>
<fileset dir="${lib.dir}" includes="*.jar" />
</classpath>
</taskdef>
<wsgen outputDirectory="${basedir}/src/" wsdl="${wsdl.path}" package="test.client" overwrite="true" binding="xmlbeans"/>
</target>
生成的客户端:
public class CBS_CustAccountInfo_IClient {
private static XFireProxyFactory proxyFactory = new XFireProxyFactory();
private HashMap endpoints = new HashMap();
public CBS_CustAccountInfo_IClient() {
}
public Object getEndpoint(Endpoint endpoint) {
try {
return proxyFactory.create((endpoint).getBinding(), (endpoint).getUrl());
} catch (MalformedURLException e) {
throw new XFireRuntimeException("Invalid URL", e);
}
}
public Object getEndpoint(QName name) {
Endpoint endpoint = ((Endpoint) endpoints.get((name)));
if ((endpoint) == null) {
throw new IllegalStateException("No such endpoint!");
}
return getEndpoint((endpoint));
}
public Collection getEndpoints() {
return endpoints.values();
}
}