通过JaxB JXC为XSD序列生成数组

Pet*_*ich 5 java xsd axis2 jaxb xjc

我有一个XSD描述了一些复杂类型的序列,例如

<xs:complexType name="Catalog">
  <xs:sequence>
    <xs:element name="Category" minOccurs="0" maxOccurs="unbounded">
      <xs:complexType>
        <xs:sequence>
          <xs:element type="xs:string" name="ParentCategoryIDRef"/>
          <xs:element type="xs:string" name="Method"/>
        </xs:sequence>
      <xs:complexType>
    </xs:element>
  </xs:sequence>
<xs:complexType>
Run Code Online (Sandbox Code Playgroud)

现在,当我使用JaxBs XJC将其转换为Java类时,它将java.util.List在我的Catalog类中生成一个字段和getter/setter Category.

但是,在使用java2wsdl的Axis2 Web服务中使用它需要的是Arrays之类的Category[].

我对JaxB绑定有点熟悉,并且已经尝试使用以下方法指定集合类型:

<jaxb:property collectionType="Category[]"/>
Run Code Online (Sandbox Code Playgroud)

导致代码无效,因为它仍然使用a java.util.List,但带有构造函数new Category[]<Category>.

当然,我总是可以在生成后编辑生成的代码,但是当我尝试重新生成代码时,这会导致问题.

我现在得到的是:

public class Catalog {
  @XmlElement(name = "Category")
  protected List<Category> category;
}
Run Code Online (Sandbox Code Playgroud)

我想要的是:

public class Catalog {
  @XmlElement(name = "Category")
  protected Category[] category;
}
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?我目前正在使用XJC 2.2.6和Axis2 1.6.2.

rob*_*rob 1

我认为你需要使用 javaType 标签:

<xs:complexType name="catalog">
        <xs:sequence>
            <xs:element name="category" type="ns:Category" >
                <xs:annotation>
                    <xs:appinfo>
                        <jxb:javaType name="Category[]"/>
                    </xs:appinfo>
                </xs:annotation>
            </xs:element>
        </xs:sequence>
    </xs:complexType>
Run Code Online (Sandbox Code Playgroud)

生成以下类:

public class Catalog {

        @XmlElement(required = true, type = Category.class)
        protected Category[] category;

        public Category[] getCategory() {
            return category;
        }

        public void setCategory(Category[] value) {
            this.category = value;
        }

    }
Run Code Online (Sandbox Code Playgroud)

(使用org.apache.cxf cxf-xjc-plugin 2.6.2 maven插件)

您还需要 XSD 中类别的定义...