列表中的Cxf Wsdl2java null条目消失

jma*_*eis 12 cxf wsdl2java jax-ws jaxb jaxb-xew-plugin

我用cxf创建了一个带有xew插件的web服务客户端,用于列表展开

问题是null列表中的内容消失了.例如:

我有一个List<String>带有字符串和null-entry的请求

当请求现在到达服务器时,它只包含字符串而不是null条目.因此示例列表中只有2个条目.

这里是wsdl的一个例子:

[..]
<!-- the request -->
<xsd:element name="createGroup">
    <xsd:complexType>
        <xsd:sequence>
            <xsd:element maxOccurs="1" minOccurs="1" name="in0" nillable="true" type="xsd:string"/>
            <xsd:element maxOccurs="1" minOccurs="1" name="in1" nillable="true" type="ns2:ArrayOfRole"/>
        </xsd:sequence>
    </xsd:complexType>
</xsd:element>
[..]
<!-- the list which will be unwrapped -->
<xsd:complexType name="ArrayOfRole">
    <xsd:sequence>
        <xsd:element maxOccurs="unbounded" minOccurs="0" name="Role" nillable="true" type="xsd:String"/>
    </xsd:sequence>
</xsd:complexType>
Run Code Online (Sandbox Code Playgroud)

我正在使用maven来生成ws客户端

<properties>
    <cxf.version>3.0.5</cxf.version>
    <jaxbBasic.version>0.6.5</jaxbBasic.version>
</properties>
[..]
<plugin>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-codegen-plugin</artifactId>
    <version>${cxf.version}</version>
    <executions>
        <execution>
            <id>generate-sources</id>
            <phase>generate-sources</phase>
            <configuration>
                <sourceRoot>src/main/java</sourceRoot>
                <defaultOptions>
                    <bindingFiles>
                        <bindingFile>${basedir}/jaxbBindings.xml</bindingFile>
                        <bindingFile>${basedir}/jaxwsBindings.xml</bindingFile>
                    </bindingFiles>
                    <extraargs>
                        <!-- xew plugin for unwrapping list wrappers types NOTE: the args need to be over the others otherwise there are compilation errors -->
                        <extraarg>-xjc-Xxew</extraarg>
                        <extraarg>-xjc-Xxew:instantiate lazy</extraarg>

                        <!-- Generate toString, equals, hashcode methods -->
                        <extraarg>-xjc-Xts:style:de.company.tostring.CustomToStringStyle.DEFAULT</extraarg>
                        <extraarg>-xjc-Xequals</extraarg>
                        <extraarg>-xjc-XhashCode</extraarg>
                    </extraargs>
                </defaultOptions>
                <wsdlRoot>${ws.dirAbsolute}</wsdlRoot>
                <includes>
                    <include>*.wsdl</include>
                </includes>
            </configuration>
            <goals>
                <goal>wsdl2java</goal>
            </goals>
        </execution>
    </executions>
    <dependencies>
        <dependency>
            <groupId>org.apache.cxf.xjcplugins</groupId>
            <artifactId>cxf-xjc-ts</artifactId>
            <version>${cxf.version}</version>
        </dependency>
        <dependency>
            <groupId>org.jvnet.jaxb2_commons</groupId>
            <artifactId>jaxb2-basics</artifactId>
            <version>${jaxbBasic.version}</version>
        </dependency>
        <dependency>
            <groupId>com.github.jaxb-xew-plugin</groupId>
            <artifactId>jaxb-xew-plugin-fixed</artifactId> <!-- this is a custom version with a small modification see https://github.com/dmak/jaxb-xew-plugin/issues/44 -->
            <version>1.7-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>com.sun.xml.bind</groupId>
            <artifactId>jaxb-xjc</artifactId>
            <version>2.2.11</version>
        </dependency>
    </dependencies>
</plugin>
Run Code Online (Sandbox Code Playgroud)

jaxbBindings.xml

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<jaxb:bindings version="2.1" xmlns:jaxb="http://java.sun.com/xml/ns/jaxb">
    <jaxb:globalBindings generateElementProperty="false"/>
</jaxb:bindings>
Run Code Online (Sandbox Code Playgroud)

jaxwsBindings.xml

<jaxws:bindings xmlns:xs="http://www.w3.org/2001/XMLSchema"
            xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
            xmlns:jaxws="http://java.sun.com/xml/ns/jaxws"
            xmlns:jaxb="http://java.sun.com/xml/ns/jaxb">
    <jaxws:enableWrapperStyle>false</jaxws:enableWrapperStyle>
</jaxws:bindings>
Run Code Online (Sandbox Code Playgroud)

样品申请:

final CreateGroup create = new CreateGroup();
create.setIn0("newgroup");
final List<String> roles = new ArrayList<String>();
roles.add("testrole");
roles.add(null);
roles.add("testrole2");
create.setIn1(roles);
final SamplePortType proxy = ..;
proxy.createGroup(create);
Run Code Online (Sandbox Code Playgroud)

有没有办法让null条目仍然存在于服务器端?

jah*_*jah 3

问题是jaxb-xew-plugin生成的代码缺少列表nillable = true 的。XmlElement

由插件生成:

@XmlElementWrapper(required = true, nillable = true)
@XmlElement(name = "Role", namespace = "http://www.stackoverflow.com/example")
protected List<String> in1;
Run Code Online (Sandbox Code Playgroud)

如果您尝试添加nillable=true,它将起作用:

@XmlElementWrapper(required = true, nillable = true)
@XmlElement(name = "Role", namespace = "http://www.stackoverflow.com/example", nillable = true)
protected List<String> in1;
Run Code Online (Sandbox Code Playgroud)

所以看来插件缺少nillable包装值的属性。

我认为问题出在这个代码部分,其中 XmlElement 被“提升”到外部元素,并且该nillable属性被省略。

将以下代码片段添加到上述代码部分将解决您的问题并生成工作代码:

JExpression nillable = getAnnotationMemberExpression(xmlElementOriginalAnnotation, "nillable");
if (nillable != null) {
    xmlElementAnnotation.param("nillable", nillable);
}
Run Code Online (Sandbox Code Playgroud)

从jaxb-xew-plugin版本 1.7开始,此问题中讨论的问题已得到解决。本期跟踪该案例,以下是发布内容。