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>
|