在朋友的幫助下,寫出了一個webService的小例子,這個webService是基于axis2寫的,寫出來,作為備忘。
首先去Apache官方下載axis,我用的是axis2-1.2這個版本,最新是axis2-1.3的版本,但是1.3需要JDK1.5的支持,我本機(jī)是JDk1.4,所以我用axis2-1.2作為ws(web service)的服務(wù)。
把下載的war包放到tomcat的webapps目錄,啟動tomcat服務(wù),在瀏覽器地址欄輸入http://localhost:8080/axis2/(根據(jù)每個人的tomcat配置的不同,這個路徑可能不同) ,如果出現(xiàn)下面界面就說明ws服務(wù)沒有問題了。
下面編寫java的ws服務(wù)
先編寫服務(wù)器端的,從最簡單的hello,world開始,工程如下圖:
Hello.java
package com;
public class Hello {
public String hw() {
return "hello,world";
}
}在再src下面建一個meta-inf的文件夾,創(chuàng)建一個services.xml的文件,文件內(nèi)容如下:
<service name="Hello">
<Description>
helloword example description
</Description>
<parameter name="ServiceClass" locked="false">com.Hello</parameter>
<operation name="hw">
<messageReceiver class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
</operation>
</service>在上面的xml描述中,
com.Hello是指你建立的類名稱,
hw是對應(yīng)的方法,
Hello是對應(yīng)的服務(wù)名稱。
把這個工程打包為jar文件,然后把擴(kuò)展名jar改為aar,放到
TomCat目錄\webapp\axis2\WEB-INF\services的目錄下面,啟動tomcat服務(wù)。
在地址欄輸入:http://localhost:8080/axis2/services/listServices ,如果服務(wù)正常,在瀏覽器頁面中會出現(xiàn)處于Active狀態(tài)的Hello的服務(wù)名稱。如圖示:
客戶端調(diào)用
目前我用了2種調(diào)用方法
Client.java
package com;
import org.apache.axiom.om.OMElement;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.rpc.client.RPCServiceClient;
import javax.xml.namespace.QName;
import javax.xml.rpc.ServiceException;
import java.net.MalformedURLException;
import java.rmi.RemoteException;
import java.util.Iterator;
public class Client {
//
public static void main(String[] args) throws RemoteException, ServiceException, MalformedURLException {
/* **************** 調(diào)用方法1 *********************** */
RPCServiceClient rpcClient = new RPCServiceClient();
Options opt = new Options();
opt.setTo(new EndpointReference("http://localhost:8080/axis2/services/Hello")); //服務(wù)地址
opt.setAction("urn:hw"); //方法
rpcClient.setOptions(opt);
OMElement element = rpcClient.invokeBlocking(new QName("http://com", "hw"), new Object[]{null}); //null表示沒有參數(shù)傳遞
Iterator values = element.getChildrenWithName(new QName("http://com", "return")); //return表示有返回值
while (values.hasNext()) { //遍歷出獲取的數(shù)據(jù)
OMElement omElement = (OMElement) values.next();
System.out.println(omElement.getText());
}
/* **************** 調(diào)用方法2 *********************** */
String method = "hw";
Service service = new Service();
Call call = (Call) service.createCall();
call.setTargetEndpointAddress(new java.net.URL("http://localhost:8080/axis2/services/Hello"));
call.setOperationName(new QName("http://com/", method));
call.setUseSOAPAction(true);
call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);
call.setSOAPActionURI("http://com/GetServerList");
String k = (String)call.invoke(new Object[]{}); //因?yàn)榉祷刂凳荢tring類型,所以這里調(diào)用的返回值也是String類型
System.out.println(">>> "+k); //返回值輸出
}
}測試結(jié)果:
如果我把卡巴斯基打開,會出現(xiàn)如下的錯誤:
log4j:WARN No appenders could be found for logger (org.apache.axis2.util.Loader).
log4j:WARN Please initialize the log4j system properly.
org.apache.axis2.AxisFault: com.ctc.wstx.exc.WstxEOFException: Unexpected EOF in prolog
at [row,col {unknown-source}]: [1,0]
at org.apache.axis2.util.Utils.getInboundFaultFromMessageContext(Utils.java:486)
at org.apache.axis2.description.OutInAxisOperationClient.handleResponse(OutInAxisOperation.java:343)
at org.apache.axis2.description.OutInAxisOperationClient.send(OutInAxisOperation.java:389)
at org.apache.axis2.description.OutInAxisOperationClient.executeImpl(OutInAxisOperation.java:211)
at org.apache.axis2.client.OperationClient.execute(OperationClient.java:163)
at org.apache.axis2.client.ServiceClient.sendReceive(ServiceClient.java:528)
at org.apache.axis2.client.ServiceClient.sendReceive(ServiceClient.java:508)
at org.apache.axis2.rpc.client.RPCServiceClient.invokeBlocking(RPCServiceClient.java:75)
at com.Client.main(Client.java:34)
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:324)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:90)Exception in thread "main"