【问题标题】:JAXB: Setter methods with return types being generated from XSD files when maxOccurs="unbounded"JAXB:当 maxOccurs="unbounded" 时从 XSD 文件生成返回类型的 Setter 方法
【发布时间】:2014-05-16 21:35:24
【问题描述】:

我正在努力将 JAXB 1.0 和最大 JDK 1.6 Web 应用程序升级到 JAXB 2.0 和 JDK 1.7 支持。

当尝试在 Java 7 中运行应用程序时,我们遇到了与此相关的 JAXB 类问题:

Why did PropertyDescriptor behavior change from Java 1.6 to 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 设置有什么问题——我所有的研究都告诉我要按照我的定义来定义它。非常感谢任何帮助。

谢谢,

【问题讨论】:

    标签: java jaxb xsd jaxb2 maven-jaxb2-plugin


    【解决方案1】:

    试试这个 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>
    

    【讨论】:

    • 不幸的是,即使添加了这个附加插件,仍然会生成相同的方法。
    • 请您指定您希望生成它的正确设置器
    • 在我列出的最后一个方法中,它应该是“public void setConnectionList(int idx, Connection value)”。这是由于 JDK 1.7 的更改要求 setter 不能有返回类型。这仅影响特定于索引的设置器。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-19
    • 1970-01-01
    • 1970-01-01
    • 2012-07-12
    • 1970-01-01
    相关资源
    最近更新 更多