我在 getter 方法级别下使用 XmlElement 注释从 Java 类生成 xsd。
@XmlElement(type=Integer.class, required=true)
public int [] getTestArrayInt () { .... }
生成的 XML 元素:
<xsd:element name="testArrayInt" type="xsd:int"/>
据说 minOccurs 的默认值是 1。因此这里没有显示。 但是缺少应该为 Array 元素列出的 maxOccurs="unbounded"。 Soap UI 期望数组元素存在 maxOccurs="unbounded"。因此,在 Soap UI 中,此元素未被视为数组。
当我从注释中删除 type=Integer.class 时,我开始在 XML 中获取 maxOccurs="unbounded"。
@XmlElement(required=true) 在元素下方生成:
<xsd:element name="testArrayInt" type="xsd:int" maxOccurs="unbounded"/>
但我需要这个 type 专门用于原始数据类型。如果注释中没有 type,minOccurs=1 会丢失不需要的元素(即未设置 required =true).
有人可以帮助我吗?
最佳答案
注意:我是 EclipseLink JAXB (MOXy) JAXB (JSR-222) 的领导和成员专家组。
您描述的问题似乎出现在 EclipseLink JAXB (MOXy) 中,而不是 JAXB 引用实现中。 MOXy 是 WebLogic 12.1.1 中的默认 JAXB 提供程序(请参阅:http://blog.bdoughan.com/2011/12/eclipselink-moxy-is-jaxb-provider-in.html)。您可以使用以下错误跟踪我们在此问题上的进展。如果您是 WebLogic 客户,请输入错误以便您可以收到适当的补丁。
Java 模型
package forum13646211;
import javax.xml.bind.annotation.XmlElement;
public class Root {
private int[] testArrayInt;
@XmlElement(type=Integer.class)
public int [] getTestArrayInt () {
return testArrayInt;
}
public void setTestArrayInt(int[] array) {
this.testArrayInt = array;
}
}
模式(由 JAXB RI 生成)
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:complexType name="root">
<xs:sequence>
<xs:element name="testArrayInt" type="xs:int" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
架构(由 EclipseLink JAXB (MOXy) 生成)
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:complexType name="root">
<xsd:sequence>
<xsd:element name="testArrayInt" type="xsd:int" minOccurs="0"/>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
模式生成代码
package forum13646211;
import java.io.IOException;
import javax.xml.bind.*;
import javax.xml.transform.Result;
import javax.xml.transform.stream.StreamResult;
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(Root.class);
jc.generateSchema(new SchemaOutputResolver() {
@Override
public Result createOutput(String namespaceUri,
String suggestedFileName) throws IOException {
StreamResult result = new StreamResult(System.out);
result.setSystemId(suggestedFileName);
return result;
}
});
}
}
关于java - 数组元素的 JAXB XmlElement maxOccurs 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13646211/