java - 数组元素的 JAXB XmlElement maxOccurs 问题

标签 java xml web-services xsd jaxb

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

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

public int [] getTestArrayInt () { .... }

生成的 XML 元素:

<xsd:element name="testArrayInt" type="xsd:int"/>

据说 minOccurs 的默认值是 1。因此这里没有显示。 但是缺少应该为 Array 元素列出的 ma​​xOccurs="unbounded"。 Soap UI 期望数组元素存在 maxOccurs="unbounded"。因此,在 Soap UI 中,此元素未被视为数组。

当我从注释中删除 type=Integer.class 时,我开始在 XML 中获取 ma​​xOccurs="unbounded"

@XmlElement(required=true) 在元素下方生成:

<xsd:element name="testArrayInt" type="xsd:int" maxOccurs="unbounded"/>

但我需要这个 type 专门用于原始数据类型。如果注释中没有 typeminOccurs=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/

相关文章:

objective-c - 文档不受源代码控制,nib 显示为 xml 文本

WCF Web 服务 xml 序列化 [XmlAttributeAttribute()] 被忽略

java - 无法声明数组大小

java - Gradle 更改 JAR 中的资源文件目标位置

java - 嵌套循环和最终值

java - 使用 Swing 在 Java 中绘制多个矩形

java.lang.ClassNotFoundException : org. hamcrest.Matchers 添加依赖项后 pom.xml

xml - 使用带有命名空间的 XPath 选择 3 个元素,但按属性对其进行过滤(仅限 Firefox)

c# - MessageInspector 和 Web 服务之间的共享信息

php - 在服务模块中为 REST 服务器启用 SSL 时没有返回数据