arrayList ,victor,set,map传输在j2ee web service标准里是有限制的 ,改用数组
初步搞定:(服务如何发布就不详细说了,网上很多)
java程序用 Axis做服务时自定义类实体的传送
核心代码:
QName qn = new QName( "urn:BeanService ", "MyArryBean ");
call.registerTypeMapping(MyArryBean.class, qn,
new BeanSerializerFactory(MyArryBean.class, qn),
new BeanDeserializerFactory(MyArryBean.class, qn));
这是在客户机端的代码,用于注册服务器上面的自定义类。然后就可以在客户端实例化这个类来使用。(如果MyArryBean 与服
务类不在同一个包里面,即服务需要用 import 来引用的话,这时就要用到 :包名.MyArryBean.class)
在Axis服务端,注册自定义对象的序列化/反序列化器
服务器的server-config.wsdd:
...
<service name= "ArrayListService " provider= "java:RPC ">
<parameter name= "className " value= "com.ArrayListService "/>
<parameter name= "allowedMethods " value= "* "/>
<beanMapping languageSpecificType= "java:com.MyArryBean " qname= "ns:MyArryBean " xmlns:ns= "urn:BeanService "/>
<requestFlow>
<handler type= "loging "/>
</requestFlow>
<responseFlow>
<handler type= "loging "/>
</responseFlow>
</service>
...
服务器上面的自定义类(这个类要被传递给客户端):MyArryBean.java
package com;
import java.io.Serializable;
public class MyArryBean implements Serializable{
private static final long serialVersionUID = -6414428095965735488L;
private String str= " ";
public MyArryBean(){}
public void setBean(String str){this.str=str;}
public String getBean(){return this.str;}
}
服务器上面的服务类:ArrayListService.java
package com;
public class ArrayListService {
//这里返回的是一个int数组
public int[] myAL(){
int myAL[] = new int[5];
for( int i=0;i <5;i++){
myAL[i]=i;
}
return myAL;
}
//这里返回的是一个自定义对象
public MyArryBean myAB(String myStr){
MyArryBean _myArryB=new MyArryBean();
_myArryB.setBean(myStr);
return _myArryB;
}
}
客户端代码:ArrayListClient.java
package com;
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;
import javax.xml.namespace.QName;
public class ArrayListClient {
public static void main(String[] args) {
try {
String wsdlUrl = "http://127.0.0.1:8080/axis/services/ArrayListService?wsdl ";
String nameSpaceUri = "http://localhost:8080/axis/services/ArrayListService ";
Service service = new Service();
Call call = null;
call = (Call) service.createCall();
//注册MyArryBean的序列化类型
QName qn = new QName( "urn:BeanService ", "MyArryBean ");
call.registerTypeMapping(MyArryBean.class, qn,
new BeanSerializerFactory(MyArryBean.class, qn),
new BeanDeserializerFactory(MyArryBean.class, qn));
//这里取得的是一个服务器int数组
call.setOperationName( "myAL ");
call.setTargetEndpointAddress(new java.net.URL(wsdlUrl));
int[] _resp = (int[])call.invoke(new java.lang.Object[] {});
for (int i=0;i <5;i++){
System.out.println( "int数组: "+_resp[i]);
}
//这里取得的是一个服务器自定义对象
//这里实例化服务器上面的映射的自定义类
MyArryBean CmyAB=new MyArryBean();
CmyAB.setBean( "这是的客户端实例化服务器的类!yes ");
System.out.println( "return value is " + CmyAB.getBean());
QName getmyABQn = new QName(nameSpaceUri, "myAB ");
call.setOperationName(getmyABQn);
call.setTargetEndpointAddress(new java.net.URL(wsdlUrl));
CmyAB = (MyArryBean) call.invoke(new Object[] { "这是调用服务器的类!web " });
System.out.println( "return value is " + CmyAB.getBean());
System.out.println( "请求信息: ");
call.getMessageContext().getRequestMessage().writeTo(System.out);
System.out.println( " ");
System.out.println( "响应信息: ");
call.getMessageContext().getResponseMessage().writeTo(System.out);
System.out.println( " ");
}
catch (Exception ex) { ex.printStackTrace(); }}}
运行结果:
int数组:0
int数组:1
int数组:2
int数组:3
int数组:4
return value is 这是的客户端实例化服务器的类!yes
return value is 这是调用服务器的类!web
请求信息:
<?xml version= "1.0 " encoding= "UTF-8 "?>
<soapenv:Envelope xmlns:soapenv= "http://schemas.xmlsoap.org/soap/envelope/ "
xmlns:xsd= "http://www.w3.org/2001/XMLSchema "
xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance ">
<soapenv:Body>
<ns1:myAB soapenv:encodingStyle= "http://schemas.xmlsoap.org/soap/encoding/ "
xmlns:ns1= "http://localhost:8080/axis/services/ArrayListService ">
<ns1:arg0 xsi:type= "soapenc:string " xmlns:soapenc= "http://schemas.xmlsoap.org/soap/encoding/ ">
这是调用服务器的类!web
</ns1:arg0>
</ns1:myAB>
</soapenv:Body>
</soapenv:Envelope>
响应信息:
<?xml version= "1.0 " encoding= "utf-8 "?>
<soapenv:Envelope xmlns:soapenv= "http://schemas.xmlsoap.org/soap/envelope/ "
xmlns:xsd= "http://www.w3.org/2001/XMLSchema "
xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance ">
<soapenv:Body>
<ns1:myABResponse soapenv:encodingStyle= "http://schemas.xmlsoap.org/soap/encoding/ "
xmlns:ns1= "http://localhost:8080/axis/services/ArrayListService ">
<myABReturn href= "#id0 "/>
</ns1:myABResponse>
<multiRef id= "id0 " soapenc:root= "0 " soapenv:encodingStyle= "http://schemas.xmlsoap.org/soap/encoding/ "
xsi:type= "ns2:MyArryBean " xmlns:soapenc= "http://schemas.xmlsoap.org/soap/encoding/ "
xmlns:ns2= "urn:BeanService ">
<bean xsi:type= "soapenc:string ">
这是调用服务器的类!web
</bean>
</multiRef>
</soapenv:Body>
</soapenv:Envelope>
|