Tuesday, April 26, 2005

AXIS custom BeanSerializer

I recently implemented a custom BeanSerializer for AXIS. The reason for implementing (or actually extending the existing) was the lack of good configurable BeanSerializer.

We needed to expose existing model objects with Collections, hiding some properties or even adding new solely for the web service. To do so, we had the choice of making data-transfer-objects only to be used for the web service, and copying values from the model objects to the DTO's or dramatically extending the BeanSerializer. We chose the latter.

I extended the existing BeanSerializer to read an XML configuration and using this both when generating the WSDL and actually serializing the beans.


<serializer>
<bean class="my.package.Bean">
<exclude>*.</exclude>
<include>owner</include>
<include>someString</include>
<introduce property="newString">serializer.introductions.BeanNewString</introduce>
<map property="owner">serializer.maps.OwnerToId</map>
</bean>
<serializer>


As you can see, the configuration example for bean my.package.Bean states:

All properties should be excluded
Then include property owner
Include property someString
Introduce new property (which is NOT available on the bean) newString
Finally the property of name owner should be mapped using the serializer.maps.OwnerToId mapper.

Including and excluded is pretty straigtforward. Introductions are merely the possibility to add new properties. An introduction is called during generation of the WSDL to set the correct type and again later (with the actual bean as parameter) when the bean is serialized.
Mappings are much like introduction in that they may choose the change the type (from a Collection to a simple array) and is also called with the object when serialized.

It works perfectly and is really useful when using existing model objects in a web service.

2 Comments:

Blogger Laurent Ploix said...

please where can I find your code ?

23/11/05 01:05  
Blogger lborupj said...

Hi Laurent,

I'm sorry to say you can't.. I wrote the custom bean-serializer as a part of a project for my previous employer and I have no rights to release the code to the public. I can however point you in the "right" direction if you decide to write our own.. It didn't take me THAT long to implement it (it acutally took longer to test it thoroughly with different kind of beans and weird compositions). Drop me an email (see address in the right border) and I'll try to write-up where and how to extend the standard axis beanserializer)

23/11/05 09:10  

Post a Comment

<< Home