How to achieve flexible polymorphism using xsd:any or xsd:anyType in jaxb

View: New views
3 Messages — Rating Filter:   Alert me  

How to achieve flexible polymorphism using xsd:any or xsd:anyType in jaxb

by metro-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

The web service I am working on has pluggable components that send optional data which is to be marshaled across transparently.  For achieving this, the response schema is defined as follows:

 <xs:complexType name="ResponseExtensionType">
  <xs:sequence>
  <xs:any  processContents="lax"/>
  </xs:sequence>
</xs:complexType>
 
 <xs:complexType name="ResponseType">
        <xs:sequence>
                    <xs:element minOccurs="0" name="methodName" type="xs:string"/>
                    <xs:element name="extInfo" type="ResponseExtensionType"/>
        </xs:sequence>
 </xs:complexType>

<xs:complexType name="AnotherType">
                        <xs:sequence>
                            <xs:element name="name" type="xs:string"/>
                            <xs:element name="value" type="xs:string"/>
                        </xs:sequence>
 </xs:complexType>

For the above ResponseExtensionType type, jaxb created the following java class:

 @XmlAccessorType(XmlAccessType.FIELD)
 @XmlType(name = "ResponseExtensionType", propOrder = {
     "any"
 })
 public class ResponseExtensionType {
 
     @XmlAnyElement(lax = true)
     protected Object any;
 
     /**
      * Gets the value of the any property.
      *
      * @return
      *     possible object is
      *     {@link Element }
      *     {@link Object }
      *    
      */
     public Object getAny() {
         return any;
     }
 
     /**
      * Sets the value of the any property.
      *
      * @param value
      *     allowed object is
      *     {@link Element }
      *     {@link Object }
      *    
      */
     public void setAny(Object value) {
         this.any = value;
     }
 
 }

Issue#1 : Error when passing Type NOT defined in the schema

When I pass an instance of MyType (it is not defined in schema) to ResponseExtensionType.setAny(), then the jaxb serialization errors out with the following error message:
class com.xxx.xxx.MyType nor any of its super class is known to this context.
        at com.xxx.xxx.JAXBValidationEventHandler.handleEvent(JAXBValidationEventHandler.java:26)
        at com.sun.xml.bind.v2.runtime.XMLSerializer.reportError(XMLSerializer.java:222)
        at com.sun.xml.bind.v2.runtime.XMLSerializer.reportError(XMLSerializer.java:239)
        at com.sun.xml.bind.v2.runtime.property.SingleReferenceNodeProperty.serializeBody(SingleReferenceNodeProperty.java:77)
        at com.sun.xml.bind.v2.runtime.ClassBeanInfoImpl.serializeBody(ClassBeanInfoImpl.java:286)
        at com.sun.xml.bind.v2.runtime.XMLSerializer.childAsXsiType(XMLSerializer.java:663)
        at com.sun.xml.bind.v2.runtime.property.SingleElementNodeProperty.serializeBody(SingleElementNodeProperty.java:113)
        at com.sun.xml.bind.v2.runtime.ElementBeanInfoImpl$1.serializeBody(ElementBeanInfoImpl.java:120)
        at com.sun.xml.bind.v2.runtime.ElementBeanInfoImpl$1.serializeBody(ElementBeanInfoImpl.java:149)
        at com.sun.xml.bind.v2.runtime.ElementBeanInfoImpl.serializeBody(ElementBeanInfoImpl.java:269)
        at com.sun.xml.bind.v2.runtime.ElementBeanInfoImpl.serializeRoot(ElementBeanInfoImpl.java:276)
        at com.sun.xml.bind.v2.runtime.ElementBeanInfoImpl.serializeRoot(ElementBeanInfoImpl.java:35)
        at com.sun.xml.bind.v2.runtime.XMLSerializer.childAsRoot(XMLSerializer.java:472)
        at com.sun.xml.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:301)
        at com.sun.xml.bind.v2.runtime.MarshallerImpl.marshal(MarshallerImpl.java:148)


Issue# 2: Error when passing Type defined in schema
     When I pass an instance of AnotherType, it is defined in schema, then the jaxb serialization errors out with the following error message:

unable to marshal type "com.xxx.xxx..AnotherType" as an element because it is missing an @XmlRootElement annotation


Please advice on how to get it working..

Thanks for help,
--Raghu
[Message sent by forum member 'raghupallikonda' (raghupallikonda)]

http://forums.java.net/jive/thread.jspa?messageID=354773

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@...
For additional commands, e-mail: users-help@...


Re: How to achieve flexible polymorphism using xsd:any or xsd:anyType in jaxb

by metro-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I am able to overcome the issue#2 ie passing an instance of AnotherType that is defined in the schema by wrapping the instance in JAXBElement...and passing it instead.  The issue# 1 still exists. Thanks. --Raghu
[Message sent by forum member 'raghupallikonda' (raghupallikonda)]

http://forums.java.net/jive/thread.jspa?messageID=354782

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@...
For additional commands, e-mail: users-help@...


Re: How to achieve flexible polymorphism using xsd:any or xsd:anyType in jaxb

by metro-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Figured the solution to issue#1. The resolution is identical to issue# 2 solution, only that I had to explicity add the non-XSD originated Java Type to JAXB context.

Thanks --Raghu
[Message sent by forum member 'raghupallikonda' (raghupallikonda)]

http://forums.java.net/jive/thread.jspa?messageID=355171

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@...
For additional commands, e-mail: users-help@...