简体   繁体   English

JAXB不会生成所有元素类

[英]JAXB doesn't generate all element classes

For a REST service I define the necessary DTOs in XML to generate JAXB object of it. 对于REST服务,我在XML中定义必要的DTO以生成它的JAXB对象。 For generation I use the built in option of the Eclipse IDE. 为了生成,我使用Eclipse IDE的内置选项。

The problem is that the classes don't get generated as expected. 问题是类没有按预期生成。 Given the XML Schema file below I'm expecting 3 classed to be generated. 鉴于下面的XML Schema文件,我预计会生成3个类。 ImageType , Image and Images . ImageTypeImageImages But the Image class for the Image element doesn't get generated. 但是不会生成Image元素的Image类。 At the moment I don't know what I'm doing wrong. 目前我不知道自己做错了什么。

<?xml version="1.0" encoding="UTF-8"?>
<schema targetNamespace="http://www.company.com/schema/v1/ImageDTO"
    elementFormDefault="qualified" xmlns="http://www.w3.org/2001/XMLSchema"
    xmlns:Q1="http://www.company.com/schema/v1/BusinessObjectDTO"
    xmlns:Q2="http://www.company.com/schema/v1/ImageDTO">

    <import schemaLocation="BusinessObjectDTO.xsd"
        namespace="http://www.company.com/schema/v1/BusinessObjectDTO"></import>

    <complexType name="ImageType" abstract="false">
        <complexContent>
            <extension base="Q1:BusinessObjectType">
                <sequence>
                    <element name="name" type="string" maxOccurs="1" minOccurs="1"></element>
                    <element name="fileName" type="string" maxOccurs="1"
                        minOccurs="1"></element>
                    <element name="thumbnailFileName" type="string" maxOccurs="1"
                        minOccurs="1"></element>
                </sequence>
            </extension>
        </complexContent>
    </complexType>

    <element name="Image" type="Q2:ImageType"></element>

    <element name="Images">
        <complexType>
            <sequence>
                <element name="ImageList" type="Q2:ImageType" maxOccurs="unbounded"
                    minOccurs="0"></element>
            </sequence>
        </complexType>
    </element>
</schema> 

You've defined Image as an element of type ImageType . 您已将Image定义为ImageType类型的元素。 Image is thus just a name used with that type. 因此, Image只是与该类型一起使用的名称。 The ImageType definition will be turned into a Java class, and when the Image element is refered somewhere in your schema, that'd result in a field of type ImageType annotated as being an XML element with the name Image . ImageType定义将转换为Java类,并且当在模式中的某处引用Image元素时,会导致ImageType类型的字段注释为名为Image的XML元素。

So say that you have... 所以说你有......

<element ref="Image" minOccurs="1" maxOccurs="1" />

somewhere in a type definition, that'd result in ... 在类型定义中的某个地方,这会导致......

@XmlElement(name="Image" ...)
ImageType image;

... in the corresponding class. ......在相应的班级。

The reason why Images did get a class definition is because you've defined it as a complexType inline. Images确实获得类定义的原因是因为您已将其定义为complexType内联。 Image refers to a type, so they're just using the corresponding class. Image是指一种类型,因此它们只是使用相应的类。 Images has an anonymous type definition, so a class must be generated to capture its structure. Images具有匿名类型定义,因此必须生成一个类来捕获其结构。

If you really need the Image class, you can force the generation of it with the following declaration: 如果您确实需要Image类,可以使用以下声明强制生成它:

<element name="Image">
    <complexType>
        <complexContent>
            <extension base="ImageType"/>
        </complexContent>
    </complexType>
</element>

如果要生成Image类,则Image必须是复杂类型,而不是Q2类型:ImageType。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM