Addressing anyType Serialization/Deserialization Errors

As per Avoid Use of 'anyType' you should avoid the use of anyType. This is due to inconsistencies with, or lack of support for, this feature depending on the environment, which may make your web service non-portable. The following information has been found to be effective for addressing one of the issues related to the use of anyType.

With some web services clients the use of the WSDL anyType type may cause an org.xml.sax.SAXException: No deserializer for anyType error. One way to resolve this error is to register a serializer and deserializer for the anyType type. This is illustrated in the code fragment in Addressing anyType Serialization/Deserialization Errors where the underlying definition, shown in the WSDL fragment in Addressing anyType Serialization/Deserialization Errors is mapped to a Java String type. In summary, the processing steps involve:

Figure 1. WSDL fragment illustrating use of anyType
...
    <xs:element name="SomeRequest" type="com:SomeResult"/>
      <xs:complexType name="SomeResult">
        <xs:sequence>
          <xs:element minOccurs="0"
                         name="anId"
                         type="xs:anyType">
            </xs:element>
        </xs:sequence>
      </xs:complexType>
  ...
Figure 2. Java client fragment for serialization/deserialization of anyType
...
// Get the client engine configuration from the locator
// to enable registration of mappings for this client.
final AxisEngine engine = locator.getEngine();
final EngineConfiguration clientEngineConfig = engine.getConfig();

// Instantiate a simple serializer and deserializer to
// map between 'anyType' and String.
final QName qnAnyType =
      new QName("http://www.w3.org/2001/XMLSchema", "anyType");
final SimpleSerializerFactory serFact =
      new SimpleSerializerFactory(String.class, qnAnyType);
final SimpleDeserializerFactory deserFact =
      new SimpleDeserializerFactory(String.class, qnAnyType);

// Now register these serializers in the client engine
// configuration. (Note that the engine config will not
// return a valid typeMapping registry until after the
// locator has created a service.)
final TypeMappingRegistry tmReg =
      clientEngineConfig.getTypeMappingRegistry();
final TypeMapping typeMapping = tmReg.getOrMakeTypeMapping("");
typeMapping.register(String.class, qnAnyType,
                     serFact, deserFact);
tmReg.register("", typeMapping);
// The service is now able to handle the anyType data type.

// The remainder of the client method would simply
// invoke the service normally.
...