简体   繁体   English

JAXB:当maxOccurs =“ unbounded”时,从XSD文件生成返回类型的Setter方法

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

I am working on upgrading a JAXB 1.0 and max JDK 1.6 web application to JAXB 2.0 and JDK 1.7 support. 我正在将JAXB 1.0和max JDK 1.6 Web应用程序升级到JAXB 2.0和JDK 1.7支持。

When trying to run the application in Java 7, we run into issues with JAXB classes which appears to be related to this: 尝试在Java 7中运行该应用程序时,我们遇到了与此有关的JAXB类问题:

Why did PropertyDescriptor behavior change from Java 1.6 to 1.7? 为什么PropertyDescriptor行为从Java 1.6更改为1.7?

It seems like the change made in JDK 1.7 was to not support any setters that had a return type other than void. 似乎在JDK 1.7中所做的更改是不支持具有void以外的返回类型的任何setter。 I thought switching to JAXB 2.0 would cause these setters to no longer be generated, but they are still getting generated and causing issues. 我以为切换到JAXB 2.0会导致不再生成这些设置器,但是它们仍在生成并引起问题。

For our application, we define XSD files and then as part of our maven build process we generate the java files from these XSDs. 对于我们的应用程序,我们定义XSD文件,然后在maven构建过程中,从这些XSD生成Java文件。

Here is the complexType that I am trying to create a list of: 这是我要创建的列表的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>

Here is the line where I am defining it in the main object: 这是我在主要对象中定义的行:

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

And here is what gets generated in the JAXB object for this field: 这是在此字段的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;
}

The last method is what it is complaining about - we are trying to set a specific index of the connection list, but it finds the setConnectionList method and does not like that it has a return type of Connection. 最后一个方法是它所抱怨的-我们正在尝试设置连接列表的特定索引,但是它找到了setConnectionList方法,并且不喜欢它的返回类型为Connection。

Here is what I have set in the pom.xml that is generating these classes: 这是我在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>

Does anyone know how to get JAXB to not generate setters with return types for array setters? 有谁知道如何使JAXB不生成具有数组设置器返回类型的设置器? I don't see anything wrong with my XSD setup - all of my research tells me to be defining it as I have. 我的XSD设置没有任何问题-我的所有研究都告诉我要按自己的定义进行定义。 Any help is much appreciated. 任何帮助深表感谢。

Thanks, 谢谢,

Dan

try with this maven plugin configuration : 尝试使用此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.

相关问题 JAXB - 在xsd:all中具有无界maxOccurs的元素 - JAXB - an element with unbounded maxOccurs inside an xsd:all 如何使用JAXB从Java中获取XSD的minOccurs / maxOccurs值? - How to get the minOccurs / maxOccurs values from the XSD in Java using JAXB? 是否可以让JAXB忽略元素中的元素顺序 <sequence> 一个或多个元素是列表时(maxOccurs = unbounded)? - Is it possible to have JAXB ignore order of elements in a <sequence> when one or more elements is a list (maxOccurs=unbounded)? 取消编组maxOs通过jaxb发生具有属性的无界元素 - Unmarshalling maxOccurs unbounded elements having attributes through jaxb XSD的JAXB生成的类中的错误 - errors in classes generated by JAXB from XSD 通过XSD def将逻辑代码插入生成的JAXB Java文件中 - Insert Logic code into generated JAXB java files by XSD def 没有使用Maven插件从xsd生成JAXB类 - No JAXB Classes Generated from xsd using Maven Plugin XSD - 如何从 JAXB 处理中删除一些生成的多余类? - XSD - how to remove some generated excess classes from JAXB processing? 从xsd:choice元素自定义JAXB 2.0生成的方法名称 - Customize JAXB 2.0 generated method names from a xsd:choice element 将访问例程添加到由XSD生成的JAXB生成的类集中 - Adding access routines to a JAXB generated set of classes originating from an XSD
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM