AXIS传递包含javaBean对象的List对象
作者:台州电大工程技术系 changleqy (changleqy@163.com)
这篇文章主要介绍的是关于通过web service传递含有bean对象的List的一个实例。
关于web Service的基础知识,本文不再详细介绍,可参考我转载的另一博文(http://blog.csdn.net/changleqy/archive/2008/07/28/2726282.aspx)
一,新建OrderBean.java,编译成OrderBean.class,放置于../axis/WEB-INF/classes下
//OrderBean.java
public class OrderBean {
private String name;
private int age;
public OrderBean() {
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
二、编写server程序,编译成ListOrder.class后,放于../axis/WEB-INF/classes下
//ListOrder.java
import java.util.ArrayList;
public class ListOrder{
public ListOrder(){
}
public ArrayList getListOrder(){
OrderBean bean1=new OrderBean();
bean1.setAge(10);
OrderBean bean2=new OrderBean();
bean2.setAge(20);
ArrayList list = new ArrayList();
list.add(bean1);
list.add(bean2);
return list;
}
}
三,配置deploy.wsdd,并通过命令(java -cp %AXISCLASSPATH% org.apache.axis.client.AdminClient deploy.wsdd)发布服务
//deploy.wsdd
<deployment xmlns="http://xml.apache.org/axis/wsdd/"
xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">
<service name="ListOrder" provider="java:RPC">
<parameter name="className" value="ListOrder"/>
<parameter name="allowedMethods" value="*"/>
<beanMapping qname="myNS:Order" xmlns:myNS="urn:AnalyzeOrder"
languageSpecificType="java:OrderBean"/>
</service>
</deployment>
四,编写client,运行!
//ListOrderClient.java
package test.backup;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;
import java.util.ArrayList;
import java.util.List;
public class ListOrderClient {
public ListOrderClient() {
}
public static void main(String [] args) throws Exception
{
String targetEendPoint =
"http://localhost:8080/axis/services/ListOrder";
Service service = new Service();
Call call = (Call) service.createCall();
QName qn = new QName("urn:AnalyzeOrder", "Order");
call.registerTypeMapping(OrderBean.class, qn,
new org.apache.axis.encoding.ser.
BeanSerializerFactory(OrderBean.class, qn),
new org.apache.axis.encoding.ser.
BeanDeserializerFactory(OrderBean.class, qn));
call.setTargetEndpointAddress(new java.net.URL(targetEendPoint));
call.setOperationName(new QName("ListOrder", "getListOrder"));
call.setReturnType(new QName("ArrayList"), ArrayList.class);
ArrayList list = (ArrayList) call.invoke(new Object[] {});
for (int i = 0; i < list.size(); i++) {
OrderBean order =(OrderBean) list.get(i);
System.out.println(order.getAge());
}
}
}