WEB开发网
开发学院软件开发Java 基于Axis的自定义对象序列化 阅读

基于Axis的自定义对象序列化

 2007-12-23 12:25:01 来源:WEB开发网   
核心提示:作者的blog:http://www.matrix.org.cn/blog/liu2liu2/由于最近公司采用Web Services技术做企业应用集成(EAI)相关的项目,有关技术人员反应采用Axis不能传递用户自定义对象,基于Axis的自定义对象序列化,错误信息大体如下:AxisFaultfaultCode: {h

  作者的blog:http://www.matrix.org.cn/blog/liu2liu2/

由于最近公司采用Web Services技术做企业应用集成(EAI)相关的项目,有关技术人员反应采用Axis不能传递用户自定义对象。

错误信息大体如下:
AxisFault
faultCode: {http://schemas.xmlsoap.org/soap/envelope/}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:
{http://xml.apache.org/axis/}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 name="SimpleObjectService" PRovider="java:RPC"> <parameter name="allowedMethods" value="*"/> <parameter name="className" value="com.whcyit.axis.SimpleObjectService"/> <beanMapping languageSpecificType="java:com.whcyit.axis.SimpleObject" qname="ns:SimpleObject" xmlns:ns="urn:BeanService"/></service>


注意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 = "http://localhost:8080/axis/services/SimpleObjectService?wsdl";  String nameSpaceUri = "http://localhost:8080/axis/services/SimpleObjectService";  
//String wsdlUrl = "http://10.68.19.24:8080/sample/services/SimpleObjectService?wsdl";  
//String nameSpaceUri = "http://10.68.19.24:8080/sample/services/SimpleObjectService";  

//创建调用对象  
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
//服务端配置文件

<?xml version="1.0" encoding="UTF-8"?>
<deployment xmlns="http://xml.apache.org/axis/wsdd/" xmlns:java="http://xml.apache.org/axis/wsdd/providers/java"> <globalConfiguration> <parameter name="adminPassWord" value="admin"/> <parameter name="attachments.Directory" value="C:\eclipse\workspace\bookstore\WEB-INF\attachments"/> <parameter name="attachments.implementation" value="org.apache.axis.attachments.AttachmentsImpl"/> <parameter name="sendXsiTypes" value="true"/> <parameter name="sendMultiRefs" value="true"/> <parameter name="sendXMLDeclaration" value="true"/> <parameter name="axis.sendMinimizedElements" value="true"/> <requestFlow>  <handler type="java:org.apache.axis.handlers.JWSHandler">  <parameter name="scope" value="session"/>  </handler>  <handler type="java:org.apache.axis.handlers.JWSHandler">  <parameter name="scope" value="request"/>  <parameter name="extension" value=".jwr"/>  </handler> </requestFlow> </globalConfiguration> <handler name="LocalResponder" type="java:org.apache.axis.transport.local.LocalResponder"/> <handler name="Authenticate" type="java:org.apache.axis.handlers.SimpleAuthenticationHandler"/> <handler name="URLMapper" type="java:org.apache.axis.handlers.http.URLMapper"/> <service name="Version" provider="java:RPC"> <parameter name="allowedMethods" value="getVersion"/> <parameter name="className" value="org.apache.axis.Version"/> </service> <service name="SimpleObjectService" provider="java:RPC"> <parameter name="allowedMethods" value="*"/> <parameter name="className" value="com.whcyit.axis.SimpleObjectService"/> <beanMapping languageSpecificType="java:com.whcyit.axis.SimpleObject" qname="ns:SimpleObject" xmlns:ns="urn:BeanService"/> </service> <service name="AdminService" provider="java:MSG"> <parameter name="allowedMethods" value="AdminService"/> <parameter name="enableRemoteAdmin" value="false"/> <parameter name="className" value="org.apache.axis.utils.Admin"/> <namespace>http://xml.apache.org/axis/wsdd/</namespace> </service> <transport name="local"> <responseFlow>  <handler type="LocalResponder"/> </responseFlow> </transport> <transport name="http"> <requestFlow>  <handler type="URLMapper"/>  <handler type="java:org.apache.axis.handlers.http.HTTPAuthHandler"/> </requestFlow> </transport></deployment>
进入讨论组讨论。

(出处:http://www.cncms.com)


Tags:基于 Axis 定义

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接