Webservice 传递对象数据
提前准备事宜:Eclipse下配置好Axis的开发环境。
即,在Eclipse下创建了项目,项目可以正常运行axis
具体准备事宜可以参看 “
简单webservice 数据传输 ”
本文参考文章地址列表:
http://hi.baidu.com/772333621/blog/item/b8a62a0803a39aef3ac7633d.html
http://topic.csdn.net/u/20090409/16/bdbe9bc3-9ad5-43ac-bc96-53a937f52a19.html
http://www.cnblogs.com/cy163/archive/2008/04/11/1147648.html
http://liuchunqing2001.blog.163.com/blog/static/308229120084942952682/
一、编写需要传递的对象Box.java
package com.mfanw;
public class Box {
public int x = 0;
public int y = 0;
public Box(int x, int y) {
this.x = x;
this.y = y;
}
}
二、编写Axis服务类GetBox.java
package com.mfanw;
public class GetBox {
public Box getBox(String x) {
int a = Integer.parseInt(x);
return new Box(a, a);
}
}
三、配置server-config.wsdd添加一个service
<service name="getBox" provider="java:RPC">
<parameter name="allowedMethods" value="*"/>
<parameter name="className" value="com.mfanw.GetBox"/>
<beanMapping languageSpecificType="java:com.mfanw.Box" qname="ns1:Box" xmlns:ns1="urn:BeanService"/>
</service>
注意beanMapping的具体配置:
languageSpecificType属性指定JavaBean类文件位置,
qname属性指定JavaBean类的名字,其他配置项是固定的。
四、编写测试程序TestGetBox.java
package com.mfanw;
import java.net.URL;
import javax.xml.namespace.QName;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.ser.BeanDeserializerFactory;
import org.apache.axis.encoding.ser.BeanSerializerFactory;
public class TestGetBox {
public static void main(String[] args) {
try {
String endpoint = "http://localhost:端口/项目名称/services/getBox?wsdl";
Service service = new Service();
Call call = (Call) service.createCall();
call.setTargetEndpointAddress(new URL(endpoint));
QName qn = new QName("urn:BeanService","ns1:Box");
call.registerTypeMapping(Box.class, qn, new BeanSerializerFactory(Box.class,qn), new BeanDeserializerFactory(Box.class,qn));
call.setOperationName(new QName("urn:BeanService", "getBox"));
call.setReturnType(qn, Box.class);
call.addParameter("x", org.apache.axis.Constants.XSD_STRING, javax.xml.rpc.ParameterMode.IN);
Box ret = (Box) call.invoke(new Object[] {"5"});
System.out.println("返回的数据是:x=" + ret.x + ", y=" + ret.y);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
运行TestGetBox.java查看运行结果。
注意:call.setReturnType(qn, Box.class);设置了返回类型之后,必须进行call.addParameter参数设置
由于Box是对象类型,必须在Axis中注册Box类型才可以被识别,即call.registerTypeMapping