Generating java classes using maven-jaxb2-plugin

2019-07-04 22:01发布

问题:

I am very new to Jaxb and this maven-jaxb2 plugin

Here is my .xsd file :

<xs:element name="user" type="user" />
<xs:element name="userList" type="userList" />

<xs:complexType name="user">
    <xs:all>
        <xs:element name="id" type="xs:long" minOccurs="0" />
        <xs:element name="name" type="xs:string" />
        <xs:element name="registrationDate" type="xs:dateTime" />
    </xs:all>
</xs:complexType>

<xs:complexType name="userList">
    <xs:sequence>
        <xs:element name="user" type="user" minOccurs="0" maxOccurs="unbounded" />
    </xs:sequence>
</xs:complexType>

Here is my .xjb file :

<!-- Annotate @XmlRootElement Annotation for all the classes, that matches 
        in the .xsd with complexType -->
    <jaxb:bindings schemaLocation="schema.xsd" node="/xs:schema">
        <jaxb:bindings node="xs:complexType['1'='1']" multiple="true"
            required="false">
            <annox:annotate>
                <annox:annotate annox:class="javax.xml.bind.annotation.XmlRootElement"
                    name="user" />
            </annox:annotate>
        </jaxb:bindings>
    </jaxb:bindings>

and when it generates it gives the java class somewhat like :

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

})
@Entity
@XmlRootElement(name = "${@name}")
@Table(schema = "schemaName", uniqueConstraints = {

}, name = "user_")
public class User
    implements Serializable, ToString
{

    private final static long serialVersionUID = 1L;
    protected Long id;

where it should give name = object name such as here "user" I know if I write in bindings like this it will genearate

<jaxb:bindings schemaLocation="schema.xsd" node="/xs:schema">
        <jaxb:bindings node="xs:complexType[@name='user']">
            <annox:annotate>
                <annox:annotate annox:class="javax.xml.bind.annotation.XmlRootElement" name="user" />
            </annox:annotate>
        </jaxb:bindings>
        <jaxb:bindings node="xs:complexType[@name='userList']">
            <annox:annotate>
                <annox:annotate annox:class="javax.xml.bind.annotation.XmlRootElement" name="userList" />
            </annox:annotate>
        </jaxb:bindings>
    </jaxb:bindings>
</jaxb:bindings>

but i dont want to repeat the same code for user and userlist is there any way I can do by annotations or defining regular expression. Please suggest some way.

I just wanted to know what to write in this code

<annox:annotate>
    <annox:annotate annox:class="javax.xml.bind.annotation.XmlRootElement"
                    name="user" />
</annox:annotate>

so that when it creates user class it show @xmlRootElement(name='user') and similary for other class @xmlRootElement(name='userList') ans similarly for others.

回答1:

You should use

<jxb:bindings node="xs:complexType" multiple="true">
  <annox:annotate>
    <annox:annotate annox:class="javax.xml.bind.annotation.XmlRootElement" />
  </annox:annotate>
</jxb:bindings>

i.e. leave the name attribute unspecified in the annox:annotate element.

This puts a @XmlRootElement annotation with no properties on the class generated for each complex type, in this case:

@XmlRootElement
public class User {
...
}

@XmlRootElement
public class UserList {
...
}

Because of the way @XmlRootElement works, this is all you need. If you don't specify a value for the name element, it is derived from the class name by default (see the Oracle docs - examples 1 & 2), which is the same as your complex type name.

In other words, the above code is exactly the same as this.

@XmlRootElement(name="user")
public class User {
...
}

@XmlRootElement(name="userList")
public class UserList {
...
}

Which is exactly what you want.

Just an additional note - the xpath selector node="xs:complexType['1'='1']" is unnecessary - you can just use node="xs:complexType" to get all the <xs:complexType> elements.