JAXB schema for Java XmlRootElement different name and class name

I have an xsd schema from which I am generating some Java classes. I am using jaxb for a generation.

I want to be able to generate a class annotated with @XmlRootElement

, but I want the name @XmlRootElement property to be different from the generated class name.

In my xsd, I define the following:

<xs:element name="customer">
    <xs:complexType>
        <xs:sequence>
         ....
        </xs:sequence>
     </xs:complexType>
</xs:element>

      

This code snippet generates the following java class:

@XmlRootElement(name = "customer")
public class Customer {
...
}

      

The name property is the @XmlRootElement

same as the name of the generated class. I want the generated class name to be CustomerReques

t.

I tried to use definition jaxb:class

to change the cluster name. This parameter actually changes the class name, but removes the annotation @XmlRootElement

and I need it to be present.

Next xsd:

<xs:element name="customer">
    <xs:complexType>
        <xs:annotation>
                <xs:appinfo>
                    <jaxb:class name="CustomerRequest"/>
                </xs:appinfo>
            </xs:annotation>
        <xs:sequence>
        </xs:sequence>
    </xs:complexType>
</xs:element>

      

Creates this class:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "customer", propOrder = {

})
public class CustomerRequest {
}

      

How can I make the annotation property name @XmlRootElement

different from the generated class name without losing the annotation?

Solution update: User Xstian suggested correct solution using external bindings. For more reference, I'm updating my own post with a solution converted to use inline bindings:

 <xs:element name="customer">
        <xs:complexType>
            <xs:annotation>
                <xs:documentation>Request object for the operation that checks if a customer profile exists.</xs:documentation>
                <xs:appinfo>
                        <annox:annotate>
                            <annox:annotate annox:class="javax.xml.bind.annotation.XmlRootElement" name="customer"/>
                        </annox:annotate>
                        <jaxb:class name="CustomerRequest"/>
                    </xs:appinfo>
                </xs:annotation>
            <xs:sequence>
            </xs:sequece>
    </xs:complexType>
</xs:element>

      

+3


source to share


1 answer


I suggest you use these bindings

<bindings version="2.0" xmlns="http://java.sun.com/xml/ns/jaxb"
    xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
    xmlns:annox="http://annox.dev.java.net"
    xmlns:namespace="http://jaxb2-commons.dev.java.net/namespace-prefix">
    <bindings schemaLocation="../your.xsd">

        <bindings node="//xs:element[@name='customer']//xs:complexType">
            <annox:annotate>
                <annox:annotate annox:class="javax.xml.bind.annotation.XmlRootElement"
                    name="customer" namespace="yourNamespaceIfYouWant">
                </annox:annotate>
            </annox:annotate>
            <class name="CustomerRequest"/>
        </bindings>

    </bindings>
</bindings>

      



Class

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
    "header"
})
@XmlRootElement(name = "customer", namespace = "yourNamespaceIfYouWant")
public class CustomerRequest

      

+4


source







All Articles