一.生成方式选择
接收到服务端第三方给的wsdl文件,需要在本地生成客户端调用。调用或生成客户端的方式有很多种,可能你会使用eclipse上的插件、IDEA上的插件、使用xfire简单的调用方式等,会碰到各种问题,就是生成不了。其实选择哪种方式,要根据wsdl定义stype和use的方式。
stype描述了服务调用方式:rpc或document,use定义了类型:encoded或literal
二.wsdl文件的片段
<wsdl:message name="sigResponse">
<wsdl:part name="sigReturn" type="soapenc:string"/>
</wsdl:message>
<wsdl:message name="sigRequest">
<wsdl:part name="province" type="soapenc:string"/>
<wsdl:part name="caller" type="soapenc:string"/>
<wsdl:part name="called" type="soapenc:string"/>
<wsdl:part name="sigType" type="soapenc:string"/>
<wsdl:part name="sigTime" type="soapenc:string"/>
</wsdl:message>
<wsdl:portType name="SigMessageDellImpl">
<wsdl:operation name="SigMsg" parameterSigMsg="province caller called sigType sigTime">
<wsdl:input message="impl:sigRequest" name="sigRequest"/>
<wsdl:output message="impl:sigResponse" name="sigResponse"/>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="sigMessageServicesSoapBinding" type="impl:SigMessageDellImpl">
<wsdlsoap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="SigMsg">
<wsdlsoap:operation soapAction=""/>
<wsdl:input name="sigRequest">
<wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://impl.service.client.com" use="encoded"/>
</wsdl:input>
<wsdl:output name="sigResponse">
<wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://*.*.*.*:*/SMSWebService/services/sigMessageServices" use="encoded"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="SigMessageDellImplService">
<wsdl:port binding="impl:sigMessageServicesSoapBinding" name="sigMessageServices">
<wsdlsoap:address location="http://*.*.*.*:*/SMSWebService/services/sigMessageServices"/>
</wsdl:port>
</wsdl:service>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
三.碰到的问题
1.我建议调用不是自己生成的服务端的wsdl,最好使用wsimport方式,调用后报错:Use of SOAP Encoding is not supported
xiaoxiangdeMacBook-Pro:~ Simons$ wsimport -keep -p zhenzhen -d ~ http://*.*.*.*/SMSWebService/services/sigMessageServices/?wsdl
parsing WSDL...
[ERROR] "Use of SOAP Encoding is not supported.
SOAP extension element on line 48 in http://*.*.*.*/SMSWebService/services/sigMessageServices/?wsdl has use="encoded" "
Failed to parse the WSDL.
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
原因:这和JDK版本有关,进入到jdk的bin目录下,查看JAX-WS的版本,而Version 2 of JAX-WS does not support rpc/encoded anymore,就是wsdl生成时选择了rpc/encoded方式(仔细查看wsdl可以看到style=”rpc”,use=”encoded”),JDK1.7中的JAX-WS已经不支持了。
xiaoxiangdeMacBook-Pro:bin Simons$ wsimport -version
JAX-WS RI 2.2.4-b01
- 1
- 2
四.解决方案
1.使用Apache Axis1.0中的WSDL2Java来生成
java org.apache.axis.wsdl.WSDL2Java http://someurl?WSDL
- 1
另外调用WSDL2Java需要相关jars,使用-cp添加进来,不支持使用通配符。提醒:linux下jar之间使用冒号隔开:,windows下jar之间使用分号隔开
java -cp mail-1.4.jar;saaj-api-1.3.jar;jaxrpc-1.1.jar;commons-discovery-0.2.jar;commons-logging-1.1.jar;axis-1.4.jar;activation-1.1.jar;wsdl4j-1.4.jar org.apache.axis.wsdl.WSDL2Java http://*.*.*.*/SMSWebService/services/sigMessageServices?wsdl
- 1
jar包可以在maven repository去下载,分别是:
mail-1.4.jar;
saaj-api-1.3.jar;
jaxrpc-1.1.jar;
commons-discovery-0.2.jar;
commons-logging-1.1.jar;
axis-1.4.jar;
activation-1.1.jar;
wsdl4j-1.4.jar
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
生成文件:
SigMessageDellImpl.java
SigMessageDellImplService.java
SigMessageDellImplServiceLocator.java
SigMessageServicesSoapBindingStub.java
- 1
- 2
- 3
- 4
2.调用方式如下,不是直接new stub
public class TestClient {
public static void main(String[] args) throws Exception{
SigMessageDellImplService locator=new SigMessageDellImplServiceLocator();
SigMessageServicesSoapBindingStub stub=(SigMessageServicesSoapBindingStub)locator.getsigMessageServices();
String result=stub.sigMsg("310000","02163630000","18000000000","8","2016-08-03 22:08:08");
System.out.println("result:"+result);
}
}