注意service/beanMapping的定义。languageSpecificType中定义使用那个类, qname定义类的名称。
2. 在Axis客户端,注册自定义对象的序列化/反序列化器。
核心代码如下:
//创建调用对象
Service service = new Service();
Call call = null;
call = (Call) service.createCall();
//注册SimpleObject的序列化类型
QName qn = new QName("urn:BeanService","SimpleObject");
call.registerTypeMapping(SimpleObject.class, qn,
new BeanSerializerFactory(SimpleObject.class, qn),
new BeanDeserializerFactory(SimpleObject.class, qn));
全部代码示例如下:
//SimpleObject.java
//Web Service对象传递的对象
package com.whcyit.axis;
import java.io.Serializable;
public class SimpleObject implements Serializable {
/**
*
*/
private static final long serialVersionUID = -6414428095965735488L;
private int i = 0;
private float f = 0.0f;
private String str = "";
public SimpleObject() {
}
public float getFloat() {
return f;
}
public void setFloat(float f) {
this.f = f;
}
public int getInt() {
return i;
}
public void setInt(int i) {
this.i = i;
}
public String getString() {
return str;
}
public void setString(String str) {
this.str = str;
}
}
// SimpleObjectService.java
//服务程序
package com.whcyit.axis;
public class SimpleObjectService {
public String sayHello(String name) {
return "Hello " + name +"!";
}
public String getString(SimpleObject obj) {
System.out.println("SimpleObjectService getString invoked!");
return "return by serviced:"+obj.getString();
}
public SimpleObject createSimpleObject(int i,float f,String str) {
System.out.println("SimpleObjectService createSimpleObject invoked!");
SimpleObject so = new SimpleObject();
so.setInt(i);
so.setFloat(f);
so.setString(str);
return so;
}
}
// SimpleObjectClient.java
//客户端调用文件
package com.whcyit.axis;
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 SimpleObjectClient {
/**
* @param args
*/
public static void main(String[] args) {
String ret = "";
try {
String wsdlUrl = "";
String nameSpaceUri = "";
//String wsdlUrl = "";
//String nameSpaceUri = "";
//创建调用对象
Service service = new Service();
Call call = null;
call = (Call) service.createCall();
//注册SimpleObject的序列化类型
QName qn = new QName("urn:BeanService","SimpleObject");
call.registerTypeMapping(SimpleObject.class, qn,
new BeanSerializerFactory(SimpleObject.class, qn),
new BeanDeserializerFactory(SimpleObject.class, qn));
//调用sayHello
System.out.println(">>>sayHello");
call.setOperationName(new QName(nameSpaceUri, "sayHello"));
call.setTargetEndpointAddress(new java.net.URL(wsdlUrl));
ret = (String) call.invoke(new Object[] { "liu2" });
System.out.println("return value is " + ret);
//调用getDesc
System.out.println(">>>getString");
SimpleObject so = new SimpleObject();
so.setString("hello!");
QName getDescQn = new QName(nameSpaceUri, "getString");
call.setOperationName(getDescQn);
call.setTargetEndpointAddress(new java.net.URL(wsdlUrl));
ret = (String) call.invoke(new Object[] { so });
System.out.println("return value is " + ret);
//调用createSimpleObject
System.out.println(">>>createSimpleObject");
QName getObjectQn = new QName(nameSpaceUri, "createSimpleObject");
call.setOperationName(getObjectQn);
call.setTargetEndpointAddress(new java.net.URL(wsdlUrl));
SimpleObject rtnSO = (SimpleObject) call.invoke(new Object[] { new Integer(1),new Float(0.1f),"liu2" });
System.out.println("return object " + rtnSO.getString());
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
//server-config.wsdd
//服务端配置文件
wsdd/