You are viewing a plain text version of this content. The canonical link for it is here.
Posted to java-user@axis.apache.org by HOUMANI Nawal RD-BIZZ-ISS <na...@francetelecom.com> on 2004/09/29 11:48:06 UTC

Problems with custom serializer/deserializer

Hello,

I am trying to write a serializer for the Person class (with two
attributes name and age and their getter and setter) and we have an
exception when we try to display the WSDL : 
	AXIS error
Sorry, something seems to have gone wrong... here are the details:
Exception - java.lang.reflect.InvocationTargetException

Person
Hi there, this is an AXIS service!
Perhaps there will be a form for invoking the service here.

There is no clear documentation about writing a custom serializer,
please help me to solve this problem.  

Best Regards, 
Nawal 


This the source code of serializer/Deserializer  and the WSDD: 

<!-- Services from TestSerializerService WSDL service -->

  <service name="Person" provider="java:RPC" style="rpc" use="encoded">
      <parameter name="wsdlTargetNamespace" value="urn:TestPerson"/>
      <parameter name="wsdlServiceElement"
value="TestSerializerService"/>
      <parameter name="wsdlServicePort" value="Person"/>
      <parameter name="className"
value="com.test.serialiser.TestSerializerImpl"/>
      <parameter name="wsdlPortType" value="TestSerializer"/>
      <operation name="getPerson" qname="operNS:getPerson"
xmlns:operNS="urn:TestPerson" returnQName="getPersonReturn"
returnType="rtns:Person" xmlns:rtns="http://serialiser.test.com" >
        <parameter name="in0" type="tns:string"
xmlns:tns="http://www.w3.org/2001/XMLSchema"/>
      </operation>
      <parameter name="allowedMethods" value="getPerson"/>

<typeMapping xmlns:ns="http://serialiser.test.com"
        qname="ns:Person"
        type="java:com.test.serialiser.Person"
        serializer="com.test.serialiser.PersonSerFactory"
        deserializer="com.test.serialiser.PersonDeserFactory"
        encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
      />
  </service>

public class PersonSerializer implements Serializer {
	
//	 Field names from Person objects
	public static final String NAME = "name";
	public static final String AGE = "age";
	public static final QName myTypeQName = new
QName("http://serialiser.test.com", "Person");
	
//	 Qualified names for XML nodes
	 public final static QName QUALIFIED_NAME_NAME = new
QName(NAME);
	 public final static QName QUALIFIED_NAME_AGE= new QName(AGE);
	 	
	 		
	public void serialize(QName name, Attributes attributes, Object
value,
			SerializationContext context) throws IOException
{
		if(!(value instanceof Person)){
			throw new IOException("Can't serialize a " +
value.getClass().getName()+"with Person Serializer");
		}
		Person data=(Person)value;
		context.startElement(name,attributes);
		context.serialize(QUALIFIED_NAME_NAME,null,data.name);
		context.serialize(QUALIFIED_NAME_AGE,null,new
Integer(data.age));
		context.endElement();
	}
	
	public Element writeSchema(Class arg0, Types types) throws
Exception {
		Element complexType=types.createElement("complexType");
		types.writeSchemaElement(myTypeQName,complexType);
		complexType.setAttribute("name","Person");
		
		Element seq=types.createElement("sequence");
		complexType.appendChild(seq);
		
		Element name=types.createElement("element");
		name.setAttribute("name","name");
		name.setAttribute("type", "xsd:string");
		seq.appendChild(name);
		Element ageElement =types.createElement("element");
		ageElement.setAttribute("name","age");
		ageElement.setAttribute("type","xsd:int");
		seq.appendChild(ageElement);
		return complexType;
	}
	public String getMechanismType() {
		
		 return Constants.AXIS_SAX;
	}
}

public class PersonDeserializer extends DeserializerImpl {
	//	 Field names from Person objects
	public static final String NAME = "name";
	public static final String AGE = "age";
	public static final QName myTypeQName = new
QName("http://serialiser.test.com", "Person");
	private Hashtable typesByMemberName = new Hashtable();
	public PersonDeserializer() {
		typesByMemberName.put(NAME, Constants.XSD_STRING);
		typesByMemberName.put(AGE, Constants.XSD_INT);
		value = new Person("", 0);
	}
	public SOAPHandler onStartChild(String namespace, String
localName,
			String prefix, Attributes attributes,
DeserializationContext context) throws SAXException{
		QName typeQName=(QName)typesByMemberName.get(localName);
		if(typeQName==null){
			throw new SAXException("Invalid Element in
Person Struct"+localName);
		}
		Deserializer
dSer=context.getDeserializerForType(typeQName);
		try {
			dSer.registerValueTarget(new FieldTarget(value,
localName));
			
		}
		catch(NoSuchFieldException e){
			throw new SAXException(e);
		}
		if (dSer==null)
			throw new SAXException("NO deserializer for a
"+ typeQName + " ???");
		return (SOAPHandler)dSer;
		
	}
}

public class PersonDeserFactory implements DeserializerFactory {
	private Vector mechanisms;

		public Deserializer getDeserializerAs(String arg0) {
		return new PersonDeserializer();
	}	
	public Iterator getSupportedMechanismTypes() {
		if (mechanisms == null) {
			mechanisms = new Vector();
			mechanisms.add(Constants.AXIS_SAX);
		}
		return mechanisms.iterator();
	}
}

public class PersonSerFactory implements SerializerFactory{
  private Vector mechanisms;
		public Serializer getSerializerAs(String arg0) {
		
		return new PersonSerializer();
	}
	public Iterator getSupportedMechanismTypes() {
		if(mechanisms==null){
			mechanisms=new Vector();
			mechanisms.add(Constants.AXIS_SAX);
		}
		return mechanisms.iterator();
	}
}