簡體   English   中英

JAXB:當maxOccurs =“ unbounded”時,從XSD文件生成返回類型的Setter方法

[英]JAXB: Setter methods with return types being generated from XSD files when maxOccurs=“unbounded”

我正在將JAXB 1.0和max JDK 1.6 Web應用程序升級到JAXB 2.0和JDK 1.7支持。

嘗試在Java 7中運行該應用程序時,我們遇到了與此有關的JAXB類問題:

為什么PropertyDescriptor行為從Java 1.6更改為1.7?

似乎在JDK 1.7中所做的更改是不支持具有void以外的返回類型的任何setter。 我以為切換到JAXB 2.0會導致不再生成這些設置器,但是它們仍在生成並引起問題。

對於我們的應用程序,我們定義XSD文件,然后在maven構建過程中,從這些XSD生成Java文件。

這是我要創建的列表的complexType:

<xs:complexType name="connection">
    <xs:annotation>
        <xs:documentation>Common connection info - switch name, host IP name and port</xs:documentation>
    </xs:annotation>
    <xs:sequence>
        <xs:element name="switchName">
            <xs:simpleType>
                <xs:restriction base="xs:string">
                    <xs:maxLength value="4"/>
                </xs:restriction>
            </xs:simpleType>
        </xs:element>
        <xs:element name="hostIPName">
            <xs:simpleType>
                <xs:restriction base="xs:string">
                    <xs:maxLength value="32"/>
                </xs:restriction>
            </xs:simpleType>
        </xs:element>
        <xs:element name="hostIPPortNumber">
            <xs:simpleType>
                <xs:restriction base="xs:string">
                    <xs:maxLength value="5"/>
                </xs:restriction>
            </xs:simpleType>
        </xs:element>
    </xs:sequence>
</xs:complexType>

這是我在主要對象中定義的行:

<xs:element name="connectionList" type="dncommon:connection" maxOccurs="unbounded" />

這是在此字段的JAXB對象中生成的內容:

public Connection[] getConnectionList() {
    if (this.connectionList == null) {
        return new Connection[ 0 ] ;
    }
    Connection[] retVal = new Connection[this.connectionList.length] ;
    System.arraycopy(this.connectionList, 0, retVal, 0, this.connectionList.length);
    return (retVal);
}

public Connection getConnectionList(int idx) {
    if (this.connectionList == null) {
        throw new IndexOutOfBoundsException();
    }
    return this.connectionList[idx];
}

public int getConnectionListLength() {
    if (this.connectionList == null) {
        return  0;
    }
    return this.connectionList.length;
}

public void setConnectionList(Connection[] values) {
    int len = values.length;
    this.connectionList = ((Connection[]) new Connection[len] );
    for (int i = 0; (i<len); i ++) {
        this.connectionList[i] = values[i];
    }
}

public Connection setConnectionList(int idx, Connection value) {
    return this.connectionList[idx] = value;
}

最后一個方法是它所抱怨的-我們正在嘗試設置連接列表的特定索引,但是它找到了setConnectionList方法,並且不喜歡它的返回類型為Connection。

這是我在pom.xml中設置的生成這些類的內容:

<dependencies>
    <dependency>
        <groupId>javax.xml.bind</groupId>
        <artifactId>jaxb-api</artifactId>
        <version>2.2.7</version>
    </dependency>
    <dependency>
        <groupId>com.sun.xml.bind</groupId>
        <artifactId>jaxb-impl</artifactId>
        <version>2.2.7</version>
    </dependency>
</dependencies>
<build>
    <plugins>
        <plugin>
            <groupId>org.jvnet.jaxb2.maven2</groupId>
            <artifactId>maven-jaxb2-plugin</artifactId>
            <version>0.9.0</version>
            <executions>
                <execution>
                    <goals>
                        <goal>generate</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <generateDirectory>src/main/java</generateDirectory>
                <forceRegenerate>true</forceRegenerate>
            </configuration>
        </plugin>
    </plugins>
</build>

有誰知道如何使JAXB不生成具有數組設置器返回類型的設置器? 我的XSD設置沒有任何問題-我的所有研究都告訴我要按自己的定義進行定義。 任何幫助深表感謝。

謝謝,

嘗試使用此Maven插件配置:

<plugin>
            <groupId>org.jvnet.jaxb2.maven2</groupId>
            <artifactId>maven-jaxb2-plugin</artifactId>
            <version>0.8.1</version>
            <executions>
                <execution>
                    <goals>
                        <goal>generate</goal>
                    </goals>
                    <configuration>
                        <args>
                            <arg>-Xcollection-setter-injector</arg>
                        </args>
                        <plugins>
                            <plugin>
                                <groupId>net.java.dev.vcc.thirdparty</groupId>
                                <artifactId>collection-setter-injector</artifactId>
                                <version>0.5.0-1</version>
                            </plugin>
                        </plugins>
                        <specVersion>2.2</specVersion>
                        <extension>true</extension>
                        <schemaDirectory><!--your schema directory--></schemaDirectory>
                        <schemaInclude>
                            <include>*.xsd</include>
                        </schemaInclude>
                    </configuration>
                </execution>
            </executions>
        </plugin>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM