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.
You should use
i.e. leave the
name
attribute unspecified in theannox:annotate
element.This puts a
@XmlRootElement
annotation with no properties on the class generated for each complex type, in this case: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.
Which is exactly what you want.
Just an additional note - the xpath selector
node="xs:complexType['1'='1']"
is unnecessary - you can just usenode="xs:complexType"
to get all the<xs:complexType>
elements.