使用maven-jaxb2-plugin生成java类 [英] Generating java classes using maven-jaxb2-plugin
问题描述
我是Jaxb的新手,这个maven-jaxb2插件
这是我的.xsd文件:
< pre class =lang-xml prettyprint-override>
< xs:element name =usertype =user/>
< xs:element name =userListtype =userList/>
< xs:complexType name =user>
< xs:all>
< xs:element name =idtype =xs:longminOccurs =0/>
< xs:element name =nametype =xs:string/>
< xs:element name =registrationDatetype =xs:dateTime/>
< / xs:all>
< / xs:complexType>
< xs:complexType name =userList>
< xs:sequence>
< xs:element name =usertype =userminOccurs =0maxOccurs =unbounded/>
< / xs:sequence>
< / xs:complexType>
这是我的.xjb文件:
<! - 为所有类注释@XmlRootElement注释,与.xsd中的
匹配complexType - >
< jaxb:bindings schemaLocation =schema.xsdnode =/ 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>
当它生成它时,它给java类提供了类似:
XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name =user,propOrder = {
})
@Entity
@XmlRootElement(name =$ {@ name})
@Table(schema =schemaName,uniqueConstraints = {
},name =user_)
公共类用户
实现Serializable,ToString
{
private final static serialVersionUID = 1L;
protected Long id;
它应该给name =对象名称,例如这里的user我知道如果我写的是绑定像这样它将生成
< jaxb:bindings schemaLocation =schema.xsdnode = / XS:模式 >
< jaxb:bindings node =xs:complexType [@ name ='user']>
< annox:annotate>
< annox:annotate annox:class =javax.xml.bind.annotation.XmlRootElementname =user/>
< / annox:annotate>
< / jaxb:bindings>
< jaxb:bindings node =xs:complexType [@ name ='userList']>
< annox:annotate>
< annox:annotate annox:class =javax.xml.bind.annotation.XmlRootElementname =userList/>
< / annox:annotate>
< / jaxb:bindings>
< / jaxb:bindings>
< / jaxb:bindings>
但我不想为用户和用户列表重复相同的代码我有什么方法可以做到注释或定义正则表达式。请提出一些建议。
我只是想知道在这段代码中写些什么
< annox:annotate>
< annox:annotate annox:class =javax.xml.bind.annotation.XmlRootElement
name =user/>
< / annox:annotate>
这样当它创建用户类时,它会显示@xmlRootElement(name ='user')和类似的其他类@xmlRootElement(name ='userList')和其他人类似。
你应该使用
< jxb:bindings node =xs:complexTypemultiple =true>
< annox:annotate>
< annox:annotate annox:class =javax.xml.bind.annotation.XmlRootElement/>
< / annox:annotate>
< / jxb:bindings>
ie 保留名称$ c在$ code> annox:annotate
元素中未指定$ c>属性。
这将放置 @ XmlRootElement
注释,对于每种复杂类型生成的类没有属性,在这种情况下:
@ XmlRootElement
公共类用户{
...
}
@XmlRootElement
公共类UserList {
...
}
由于 @XmlRootElement
的工作原理,这就是你所需要的。如果没有为name元素指定值,则默认情况下它是从类名派生的(请参阅Oracle文档 - 示例1和2),它与您的复杂类型名称相同。
换句话说,上面的代码与此完全相同。
@XmlRootElement(name =user)
public class User {
...
}
@XmlRootElement(name =userList)
公共类UserList {
...
}
这正是你想要的。
只是一个额外的注释 - xpath选择器 node =xs:complexType ['1 '='1']
是不必要的 - 您只需使用 node =xs:complexType
即可获得所有< xs:complexType>
元素。
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
<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.
这篇关于使用maven-jaxb2-plugin生成java类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!