java WebService axis测试小例子1

来源:百度文库 编辑:神马文学网 时间:2024/05/01 04:37:04
java WebService测试小例子
2008-01-11 10:06
在朋友的帮助下,写出了一个webService的小例子,这个webService是基于axis2写的,写出来,作为备忘。
首先去Apache官方下载axis,我用的是axis2-1.2这个版本,最新是axis2-1.3的版本,但是1.3需要JDK1.5的支持,我本机是JDk1.4,所以我用axis2-1.2作为ws(web service)的服务。
把下载的war包放到tomcat的webapps目录,启动tomcat服务,在浏览器地址栏输入http://localhost:8080/axis2/(根据每个人的tomcat配置的不同,这个路径可能不同) ,如果出现下面界面就说明ws服务没有问题了。

下面编写java的ws服务
先编写服务器端的,从最简单的hello,world开始,工程如下图:

Hello.java
package com;
public class Hello {
public String hw() {
return "hello,world";
}
}
在再src下面建一个meta-inf的文件夹,创建一个services.xml的文件,文件内容如下:


helloword example description

com.Hello




在上面的xml描述中,com.Hello是指你建立的类名称,hw是对应的方法,Hello是对应的服务名称。
把这个工程打包为jar文件,然后把扩展名jar改为aar,放到TomCat目录\webapp\axis2\WEB-INF\services的目录下面,启动tomcat服务。
在地址栏输入:http://localhost:8080/axis2/services/listServices ,如果服务正常,在浏览器页面中会出现处于Active状态的Hello的服务名称。如图示:

客户端调用
目前我用了2种调用方法
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 {
/* **************** 调用方法1 *********************** */
RPCServiceClient rpcClient = new RPCServiceClient();
Options opt = new Options();
opt.setTo(new EndpointReference("http://localhost:8080/axis2/services/Hello")); //服务地址
opt.setAction("urn:hw"); //方法
rpcClient.setOptions(opt);
OMElement element = rpcClient.invokeBlocking(new QName("http://com", "hw"), new Object[]{null}); //null表示没有参数传递
Iterator values = element.getChildrenWithName(new QName("http://com", "return")); //return表示有返回值
while (values.hasNext()) { //遍历出获取的数据
OMElement omElement = (OMElement) values.next();
System.out.println(omElement.getText());
}
/* **************** 调用方法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[]{}); //因为返回值是String类型,所以这里调用的返回值也是String类型
System.out.println(">>> "+k); //返回值输出
}
}
测试结果:

如果我把卡巴斯基打开,会出现如下的错误:
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"