2

我使用下面的 getter 方法级别 XmlElement 注释从 Java 类生成 xsd。

@XmlElement(type=Integer.class, required=true)

公共 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"/>

但我需要这种类型专门用于原始数据类型。如果没有输入注释,minOccurs=1会丢失不需要的元素(即 required =true 未设置)

有人可以帮助我吗?

4

1 回答 1

1

注意: 我是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;
            }

        });

    }

}
于 2012-11-30T15:00:56.823 回答