轉,老SOAP WSDL

一.生成方式選擇

接收到服務端第三方給的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);
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章