我目前正在开发一个使用以下结构的 xsd:
<xs:attribute name="listVersionID" type="xs:normalizedString" use="required" fixed="1.0">
虽然本身没有问题,但使用起来相当烦人,因为该定义的固定值在 xsd 规范的版本之间增加,并且我们需要修改单独的常量类中的值以保持它们有效,尽管很少如果 xsd 中任何感兴趣的内容发生了变化。 xsd 在其他地方维护,因此仅更改它是没有选择的。
因此我问自己是否有一个 jaxb-plugin 或类似的插件可以将固定值属性转换为常量ala
@XmlAttribute(name = "listVersionID")
@XmlJavaTypeAdapter(NormalizedStringAdapter.class)
@XmlSchemaType(name = "normalizedString")
protected final String listVersionID = "1.0";
而不仅仅是
@XmlAttribute(name = "listVersionID")
@XmlJavaTypeAdapter(NormalizedStringAdapter.class)
@XmlSchemaType(name = "normalizedString")
protected String listVersionID;
必须手动填充。
有谁知道这样的吗?
是的,可以通过自定义 jaxb 绑定来实现,该绑定可以作为文件添加到代码生成器中。
在 jaxb 绑定中,有fixedAttributeAsConstantProperty
-属性。将其设置为 true,指示代码生成器生成具有以下属性的属性fixed
属性作为 java 常量。
有两种选择:
1.通过全局绑定:
然后将所有具有固定值的属性设置为常量
<schema targetNamespace="http://stackoverflow.com/example"
xmlns="http://www.w3.org/2001/XMLSchema"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
jaxb:version="2.0">
<annotation>
<appinfo>
<jaxb:globalBindings fixedAttributeAsConstantProperty="true" />
</appinfo>
</annotation>
...
</schema>
2.通过本地映射:
这仅定义了fixedAttributeAsConstantProperty
特定属性上的属性。
<schema targetNamespace="http://stackoverflow.com/example"
xmlns="http://www.w3.org/2001/XMLSchema"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
jaxb:version="2.0">
<complexType name="example">
<attribute name="someconstant" type="xsd:int" fixed="42">
<annotation>
<appinfo>
<jaxb:property fixedAttributeAsConstantProperty="true" />
</appinfo>
</annotation>
</attribute>
</complexType>
...
</schema>
这两个例子都应该导致:
@XmlRootElement(name = "example")
public class Example {
@XmlAttribute
public final static int SOMECONSTANT = 42;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)