由于最近公司采用Web Services技术做企业应用集成(EAI)相关的项目,有关技术人员反应采用Axis不能传递用户自定义对象。
错误信息大体如下:
AxisFault
faultCode: {}Server.userException
faultSubcode:
faultString: java.io.IOException: No serializer found for class com.whcyit.axis.SimpleObject in registry org.apache.axis.encoding.TypeMappingDelegate@da3a1e
faultActor:
faultNode:
faultDetail:
{}stackTrace:java.io.IOException: No serializer found for class com.whcyit.axis.SimpleObject in registry org.apache.axis.encoding.TypeMappingDelegate@da3a1e
at org.apache.axis.encoding.SerializationContext.serializeActual(SerializationContext.java:1429)
对于Web Services的接触已经是4年多前的事情了,基本上对于Web Services只是存在一个概念了。抱着疑问,决定自己试一下到底怎么回事?
最后通过2个多小时的测试,终于把问题解决了。解决方式如下:
1. 在Axis服务端,注册自定义对象的序列化/反序列化器
主要是修改axis安装目录下WEB-INF/server-config.wsdd文件。
注意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
//服务端配置文件
【责编:admin】