0

I'm trying to consume a REST WebService, in which requests are sent as serialized XML objects

Here is an example

https://localhost:9985/fr_FR/api.ReceiveXMLmessage?xmlString=<TDetails_XCC OpeType="SOME_OPERATION"><TeamID>SOME_ID</TeamID></TDetails_XCC>

From this, I created the following XSD schema

<complexType name="TDetails_XCC">
    <annotation>
        <appinfo>
            <jaxb:class name="TeamDetailsRequestType"/>
        </appinfo>
        <documentation>Request team details</documentation>
    </annotation>
    <complexContent>
        <extension base="local:SimpleRESTRequestType"/>
    </complexContent>
</complexType>

<complexType name="SimpleRESTRequestType">
    <sequence>
        <element name="TeamID" minOccurs="0" type="long">
            <annotation>
                <appinfo>
                    <jaxb:property name="teamId"/>
                </appinfo>
            </annotation>
        </element>
    </sequence>
    <attribute name="OpeType" type="string" default="some_operation">
        <annotation>
            <appinfo>
                <jaxb:property name="operationType"/>
            </appinfo>
        </annotation>
    </attribute>
</complexType>

This is the generated Java bean

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "TDetails_XCC")
@XmlRootElement public class TeamDetailsRequestType
    extends SimpleRESTRequestType
    implements Serializable
    {
    }

This is the code used to serialize the bean to XML

  TeamDetailsRequestType request = new TeamDetailsRequestType();
  request.setTeamId(546464L);
  request.setOperationType("SomeOperation");
  StringWriter writer = new StringWriter();
  JAXBContext context = JAXBContext.newInstance("com.mycompany.myproject.message.team");
  Marshaller marshaller = context.createMarshaller();
  marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
  marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, false);
  marshaller.marshal(request, writer);
  System.out.println(writer.toString());

This is what the marshalled bean looks like

<teamDetailsRequestType OpeType="SomeOperation">
    <TeamID>546464</TeamID>
</teamDetailsRequestType>

And this what I wished I got

<TDetails_XCC OpeType="SomeOperation">
    <TeamID>546464</TeamID>
</TDetails_XCC>

I managed to get it working by using the QName class

JAXBElement jx = new JAXBElement(new QName("TDetails_XCC"), request.getClass(), request);
marshaller.marshal(jx, System.out);

But what I would like to know is if it possible to accomplish this solely from the XSD schema?

Thank you for your time

Update

I'm guessing that the problem comes from the generated bean, because the problem is solved with the following bean

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType
@XmlRootElement(name = "TDetails_XCC")
public class TeamDetailsRequestType
    extends SimpleRESTRequestType
    implements Serializable
    {
    }

So, how can I change my XSD to make JAXB put the name property inside of @XmlRootElement instead of @XmlType?

Thanks again

1 Answers1

0

Here is the solution

<element name="TDetails_XCC">
    <annotation>
        <appinfo>
            <jaxb:class name="TeamDetailsRequestType"/>
        </appinfo>
        <documentation>Request team details</documentation>
    </annotation>
    <complexType>
        <complexContent>
            <extension base="local:SimpleRESTRequestType"/>
        </complexContent>
    </complexType>
</element>

As explained in this StackOverflow thread, one of the many solutions to generate the @XmlRootElement annotation for a ComplexType, is to declare it as an anonymous ComplexType (ie wrapped in a named element)

Community
  • 1
  • 1