因爲發現很多人現在 對webservice的調用都是死記硬背,有些代碼又寫的很長。爲了能實現跨平臺操作 我們這邊選用java來做。
一下這些都是自己原創心得,所有有什麼不對的地方望各位指出。
根據 axis2自動生成wsdl的java類,編寫客戶端
1.首先必須安裝java客戶端(這是基礎,就不說了)
2.查看webservice 這裏隨便找了2個用不同技術寫的webservice,以便做對比
2.1 用java 自帶的JAX-WS寫的 webservice:
<?xml version="1.0" encoding="UTF-8" ?>
- <!-- Published by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.2.4-b01.
-->
- <!-- Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.2.4-b01.
-->
- <definitions xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:wsp="http://www.w3.org/ns/ws-policy" xmlns:wsp1_2="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://ws.esb.bsoft.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://ws.xx.sxxxx.com/" name="WebServiceEntryService">
- <types>
- <xsd:schema>
<xsd:import namespace="http://jaxb.dev.java.net/array" schemaLocation="http://xxx.xxx.xxx.xxx:12304/WebServiceEntry?xsd=1" />
</xsd:schema>
</types>
- <message name="invoke">
<part name="appId" type="xsd:string" />
<part name="pwd" type="xsd:string" />
<part name="service" type="xsd:string" />
<part name="method" type="xsd:string" />
<part xmlns:ns1="http://jaxb.dev.java.net/array" name="param" type="ns1:stringArray" />
</message>
- <message name="invokeResponse">
<part name="return" type="xsd:string" />
</message>
- <message name="invokeWithSingleParam">
<part name="appId" type="xsd:string" />
<part name="pwd" type="xsd:string" />
<part name="service" type="xsd:string" />
<part name="method" type="xsd:string" />
<part name="param" type="xsd:string" />
</message>
- <message name="invokeWithSingleParamResponse">
<part name="return" type="xsd:string" />
</message>
- <portType name="WebServiceEntry">
- <operation name="invoke" parameterOrder="appId pwd service method param">
<input wsam:Action="http://ws.esb.bsoft.com/WebServiceEntry/invokeRequest" message="tns:invoke" />
<output wsam:Action="http://ws.esb.bsoft.com/WebServiceEntry/invokeResponse" message="tns:invokeResponse" />
</operation>
- <operation name="invokeWithSingleParam" parameterOrder="appId pwd service method param">
<input wsam:Action="http://ws.esb.bsoft.com/WebServiceEntry/invokeWithSingleParamRequest" message="tns:invokeWithSingleParam" />
<output wsam:Action="http://ws.esb.bsoft.com/WebServiceEntry/invokeWithSingleParamResponse" message="tns:invokeWithSingleParamResponse" />
</operation>
</portType>
- <binding name="WebServiceEntryPortBinding" type="tns:WebServiceEntry">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="rpc" />
- <operation name="invoke">
<soap:operation soapAction="" />
- <input>
<soap:body use="literal" namespace="http://ws.esb.bsoft.com/" />
</input>
- <output>
<soap:body use="literal" namespace="http://ws.esb.bsoft.com/" />
</output>
</operation>
- <operation name="invokeWithSingleParam">
<soap:operation soapAction="" />
- <input>
<soap:body use="literal" namespace="http://ws.esb.bsoft.com/" />
</input>
- <output>
<soap:body use="literal" namespace="http://ws.esb.bsoft.com/" />
</output>
</operation>
</binding>
- <service name="WebServiceEntryService">
- <port name="WebServiceEntryPort" binding="tns:WebServiceEntryPortBinding">
<soap:address location="http://xxx.xx.xx.xx:12304/WebServiceEntry" />
</port>
</service>
</definitions>
來現在我們來解析這個webservice。首先它有invoke(string appId,string pwd,string service,string method,String[] param) 對應的返回方法是String invokeResponse 和invokeWithSingleParam(string appId,string pwd,string service,string method) 返回 String invokeWithSingleParamResponse 這兩個方法 。
至於還不知道爲什麼是這兩個方法的的童鞋,其實就是從下往上看 webservice地址 命名空間 指定引用。。。。。。
這樣一理,看上去是蠻有調理。但是這一段沒法直接調用,那怎麼辦呢?現在就要有請我們的axis出場來解析它了。
2.2 如何解析
1.下載axis2 這裏我用的是1.5.6的版本
無需安裝,但是要在我們的環境變量裏面設置好
2.cmd命令打開
調試出windows控制檯。 查找到axis所在的Bin目錄下
輸入 wsdl2java -uri http://xxx.xxx.xxx.xx:12304/WebServiceEntry?wsdl -p com.webservice.platform -o platformClient
這時候客戶端java類就會自動創建了
現在就來解讀一下上面這段命令:
uri :不用說了webservice地址
-p: 類的包名
-o:生成的一系列文件保存的根目錄
執行完之後我們就發現多了一個目錄,而此時的客戶端文件也就創建好了
完成一半了。接下來到了我們最重要的一步,讀懂客戶端創建的webservice類。
3.怎樣去看
和看webservice的時候一樣,我們首先找到出參的地方 也就是那個出參方法。 我這裏先寫出客戶端調用的方法。待會再一一解釋。
假設我們調用的是invokeWithSingleParam 這個方法。上面說的,這個方法所對應的返回值是從InvokeWithSingleParamResponse 這裏來的。所以我們先定位InvokeWithSingleParamResponse。同時也要找到InvokeWithSingleParamResponse這個實例的對象是如何創建的。
發現果然這裏有get_return()這個方法。
找到了 那我們就開始了。先把該有的框架(最後返回的值是從哪裏返回的)寫好
package com.webservice.platform;
import com.webservice.platform.WebServiceEntryServiceStub.InvokeWithSingleParamResponse;
public class TestPlatform {
/**
* @param args
* @author H7_N18
* @time 2018-9-14下午3:28:45
* @description
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
WebServiceEntryServiceStub stub = new WebServiceEntryServiceStub();
InvokeWithSingleParamResponse invokeWithSingleParamResponse =
stub.invokeWithSingleParam(invokeWithSingleParam2);
String resXML = invokeWithSingleParamResponse.get_return();
System.out.println("返回的xml是:"+resXML);
}
}
此時這個invokeWithSingleParam2 又是怎麼來的呢?想想應該也知道這是傳遞參數值的方法,那我們go on !
在我們自動生成類的方法裏面寫了,這個類是WebServiceEntryServiceStub.InvokeWithSingleParam 也是WebServiceEntryServiceStub的一個內部類哦。
果然這些設置參數值的都在這裏。
OK 那就一氣呵成。代碼如下:
package com.webservice.platform;
import java.rmi.RemoteException;
import org.apache.axis2.AxisFault;
import com.webservice.platform.WebServiceEntryServiceStub.InvokeWithSingleParam;
import com.webservice.platform.WebServiceEntryServiceStub.InvokeWithSingleParamResponse;
public class TestPlatform {
/**
* @param args
* @author H7_N18
* @time 2018-9-14下午3:28:45
* @description
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String resXML=null;
try {
WebServiceEntryServiceStub stub = new WebServiceEntryServiceStub();
InvokeWithSingleParam invokeWithSingleParam2 = new InvokeWithSingleParam();
invokeWithSingleParam2.setAppId("appid");
invokeWithSingleParam2.setPwd("pwd");
invokeWithSingleParam2.setService("service");
invokeWithSingleParam2.setMethod("method");
invokeWithSingleParam2.setParam("<messages><setdetails><ids>name</ids>" +
"</setdetails><setdetails><ids>name</ids></setdetails></messages>");
InvokeWithSingleParamResponse invokeWithSingleParamResponse =
stub.invokeWithSingleParam(invokeWithSingleParam2);
resXML = invokeWithSingleParamResponse.get_return();
} catch (AxisFault e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("返回的xml是:"+resXML);
}
}
返回的xml:
SLF4J: This version of SLF4J requires log4j version 1.2.12 or later. See also http://www.slf4j.org/codes.html#log4j_version
log4j:WARN No appenders could be found for logger (org.apache.axis2.description.AxisService).
log4j:WARN Please initialize the log4j system properly.
返回的xml是:invoke failure:beanName[service] not found on server registry
服務返回的信息是 這個service未找到(因爲我隨便輸的)。
完成了!