Hi

I'm presuming you have a method like this one :

I've experimented a bit and I've managed to have a test producing the
expected data, but only when using an explicit JAXBElement, this one 

@Test
public void testWriteDerivedTypeJaxbElement() throws Exception {
        JAXBElementProvider provider = new JAXBElementProvider();
        Method m = CollectionsResource.class.getMethod("getBaseJaxb", new
Class[0]);
        DerivedType derived = new DerivedType();
        JAXBElement<BaseType> jaxb = new JAXBElement<BaseType>(_Base_QNAME,
BaseType.class, null, derived); 
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        provider.writeTo(jaxb, m.getReturnType(), m.getGenericReturnType(),
                       new Annotation[0], MediaType.TEXT_XML_TYPE, new
MetadataMap<String, Object>(), bos);
        String s = bos.toString();
        System.out.println(s);
        
}

produces

<?xml version="1.0" encoding="UTF-8" standalone="yes"?><ns2:base
xmlns:ns2="http://www.cisco.com/dms/xml/ns/dsmCommonService";
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
xsi:type="ns2:DerivedType"/>

public class RootResource {
  @GET
  public BaseType getType() { return new DerivedType(); }
}

I'm not sure why JAXB chooses to serialize it as

<?xml version="1.0" encoding="UTF-8" standalone="yes"?><ns2:derivedType
xmlns:ns2="http://www.cisco.com/dms/xml/ns/dsmCommonService"/>

in this case, given that ObjectFactory has a method returning
JAXBElement<BaseType>. I tried to force an ObjectFactory method returning
JAXBElement<BaseType> be used, I removed @XmlRootElement on BaseType but it
didn't make any difference. Removing @XmlRootElement on DerivedType causes a
JAXB failure - though ObjectFactory has a method returning
JAXBElement<DerivedType>.

Can you please post a question to the JAXB users list ? I'd be interested to
see what they reply. Unless our resident experts (Dan, Benson) can help ?

thanks, Sergey








qzpmwo qzpmwo wrote:
> 
> Hi,
> 
> I'm having a strange problem with CXF RESTful service marsahlling base and
> derived xsd types.
> 
> XSD:
> 
> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema";
>     xmlns:jaxb="http://java.sun.com/xml/ns/jaxb";
>     xmlns="http://www.cisco.com/dms/xml/ns/dsmCommonService";
>     targetNamespace="http://www.cisco.com/dms/xml/ns/dsmCommonService";
>     elementFormDefault="unqualified" jaxb:version="2.0">
> 
>    <xs:complexType name="BaseType">
>       <xs:sequence>
>       </xs:sequence>
>    </xs:complexType>
> 
>    <xs:complexType name="DerivedType">
>       <xs:complexContent>
>          <xs:extension base="BaseType">
>             <xs:sequence>
>             </xs:sequence>
>          </xs:extension>
>      </xs:complexContent>
>    </xs:complexType>
> 
>    <xs:complexType name="ListType">
>       <xs:sequence>
>                <xs:element name="item" type="BaseType" minOccurs="0"
> maxOccurs="unbounded" />
>       </xs:sequence>
>    </xs:complexType>
> 
>    <xs:element name="base" type="BaseType" />
>    <xs:element name="derived" type="DerivedType" />
>    <xs:element name="list" type="ListType" />
> 
> </xs:schema>
> 
> xjc has been used to jenerate java code from xsd. After that:
> 
> 1)  @XmlSeeAlso pointing to DerivedType class and @XmlRootElement
> annotations have been added to BaseType java class code
> 2) @XmlRootElement has been added to DerivedType java class code
> 3) @XmlRootElement has been added to ListType java class code
> 
> When my CXF rest service returns list of the Derived objects it produces
> xml
> correctly:
> 
> <list>
>     <base xsi:type="DerivedType">...</base>
>     ...
>     <base xsi:type="DerivedType">...</base>
> </list>
> 
> But when I'm requesting 1 instance of the Derived object XML is wrong (as
> I
> understand)
> 
> <derived>...</derived>
> 
> Since I did not specify substitution group in my opinion xml should be
> like
> in list:
> 
> <base xsi:type="DerivedType">...</base>
> 
> Could you please clarify the issue for me and may be help me to solve this
> problem.
> 
> Forgot to mention that I'm using CXF 2.2
> 
> Thanks in advance
> --- Karen
> 
> 

-- 
View this message in context: 
http://www.nabble.com/Possible-problem-with-JAXB-marshalling-tp24549727p24568009.html
Sent from the cxf-user mailing list archive at Nabble.com.

Reply via email to