The key in overriding properties of the root element of the response is to write a customer serializer. What is a serializer? A serializer converts abstract data into a format that can more easily be transmitted between endpoints. In this case, a serializer converts scalar and complex data into XML. By writing our own serializer we can tell SOAP::Lite how to construct the XML representing the response.
To create a custom serializer, you need to know a little bit about Perl OOP (Object Oriented Programing) and inheritence. The following code block shows a custom serializer which inherits all properties and behavior from the default SOAP::Lite Serializer, but overrides one of its methods/subroutines with its own:
package MySerializer;
@MySerializer::ISA = 'SOAP::Serializer';
sub envelope {
$_[2] = SOAP::Data->name($_[2])->prefix('temp')->uri('urn:TemperatureService')
if $_[1] =~ /^(?:method|response)$/;
shift->SUPER::envelope(@_);
}
This is just a simple serializer, one that changes the root element only, but a serializer could be used to serialize any part of a message (in this case a response).
You can save your serializer to its own file (in the form of a perl module), and then 'use' it. Or alternatively, you can encapsulate the package in a BEGIN { } block anywhere in your code. Either way, to tell SOAP::Lite to use your serializer as opposed to its own, you will need to do the following within your Web service instance:
SOAP::Transport::HTTP::CGI
->serializer(MySerializer->new)
->dispatch_to('MyService')
->handle;
You can find an example of a trivial temperature conversion service which utilizes this custom serializer: here.
Comments