java - JAXB:当 maxOccurs ="unbounded"时,从 XSD 文件生成具有返回类型的 Setter 方法

标签 java jaxb xsd jaxb2 maven-jaxb2-plugin

我正在努力将 JAXB 1.0 和 max 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 会导致不再生成这些 setter,但它们仍然会生成并导致问题。

对于我们的应用程序,我们定义 XSD 文件,然后作为 Maven 构建过程的一部分,我们从这些 XSD 生成 java 文件。

这是我尝试创建列表的复杂类型:

<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 不生成具有数组 setter 返回类型的 setter ?我没有发现我的 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>

关于java - JAXB:当 maxOccurs ="unbounded"时,从 XSD 文件生成具有返回类型的 Setter 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23704915/

相关文章:

java - JTextarea 作为 JTree 的节点

java - 链表栈,推到底部而不是顶部

json - Jackson Jaxb Json 与 Apache CXf

java - Maven+Spring 项目无法加载依赖的 xsd 文件

c# - 如何在文档中找到未知的 XML 命名空间?

java - Hibernate 未设置引用列

java - 缓存不适用于 Spring 3.0、Hibernate 3.6 和 EhCache 2.6.6

java - JAXB xsi :type subclass unmarshalling not working

java - jaxb可以生成这样的xml模式吗?

xml - XML 模式中的数字 ID 类型