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.