0

编辑:我已经重新格式化了这个问题,希望能让我的意图更清楚。

我正在尝试使用 JAXB 编组器将 java 对象映射到 XML,以创建对供应商 SOAP 服务的肥皂请求。我几乎没有需要在请求中显示的元素,即使它们为空或为空。我在 pom.xml 中使用jaxws-maven-plugin带有目标的插件wsimport来从 WSDL 文件生成源。

<plugin>
<groupId>com.helger.maven</groupId>
<artifactId>jaxws-maven-plugin</artifactId>
<version>2.6</version>
<executions>
    <execution>
        <goals>
            <goal>wsimport</goal>
        </goals>
        <configuration>
            <wsdlFiles>
                <wsdlFile>${project.basedir}/src/wsdl/sample/SomeSoapService.wsdl</wsdlFile>
            </wsdlFiles>
        </configuration>
        <id>wsimport-generate-cbs</id>
        <phase>generate-sources</phase>
    </execution>
</executions>
<dependencies>
    <dependency>
        <groupId>javax.xml</groupId>
        <artifactId>webservices-api</artifactId>
        <version>2.0</version>
    </dependency>
</dependencies>
<configuration>
    <sourceDestDir>${project.build.directory}/generated-sources/jaxws-wsimport</sourceDestDir>
    <xnocompile>true</xnocompile>
    <xadditionalHeaders>true</xadditionalHeaders>
    <verbose>true</verbose>
    <extension>true</extension>
</configuration>

我从这里 [https://stackoverflow.com/questions/11215485/jax-ws-to-remove-empty-tags-from-request-xml] 阅读并理解,对于具有xs:string类型的元素,空标签可以通过设置一个空字符串""作为它的值来生成。但它不能对具有诸如xs:dateTime, xs:boolean, xs:integerwhich 将XMLGregorianCalendar, BigInteger, Boolean分别产生对象的类型的元素进行。如果我将它们的值设置为 null,则不会在编组过程中生成元素。就像其他人指出的那样,它可以通过添加nillable=true来实现,@XmlElement但这将需要更改生成的源代码,除非我在第一次构建后从 maven 中删除插件,否则它将在下一次构建时被覆盖。

我当前的解决方法是将请求中需要显示的特定元素的类型更改为xs:stringWSDL 文件,并将空字符串传递给它们相应的 java 对象字段。

我的问题是,是否可以在 WSDL 端进行更改以触发 jaxws:wsimport 添加nillable=true到生成的@XmlElement字段中?我更喜欢更改 WSDL 文件而不是生成的源的原因是

  1. 我已经对 WSDL 文件进行了一些更改,以便所有更改都可以放在一个地方,更容易记录。
  2. 我想避免更改生成的源,以便我可以将它们排除在 Git 存储库之外,并防止在每次构建时被覆盖。

以下是代码的简化版本。

首先,我为 complexType Currency 生成了这个 java 类。

// Generated    
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "currency", propOrder = {
    "crossCurrency",
    "amount"
})
public class Currency {

    @XmlElement(required = true)
    protected Boolean crossCurrency;
    @XmlElement(required = true)
    protected BigDecimal amount;
}

我将字段设置为空,因为它们不存在。

requestInputRq.setCurrency(new Currency());
requestInputRa.setOtherField("Some value");

这将生成 <typ:Currency /> 但我需要像下面那样生成它。

<typ:Currency>
    <typ:crossCurrency/>
    <typ:amount/>
</typ:Currency>
4

1 回答 1

1

使用nillable属性@XmlElement并将其值设置为true

货币.java

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "currency", propOrder = {
    "isoCurrencyCode",
    "amount"
})
public class Currency {

    @XmlElement(required = true, nillable=true)
    protected Boolean crossCurrency;
    @XmlElement(required = true, nillable=true)
    protected BigDecimal amount;
}

输出

<typ:Currency>
    <typ:crossCurrency xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
    <typ:amount xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
</typ:Currency>

WSDL

添加nillable属性并将其值设置为true每个请求的元素,如下例所示,

<xsd:element name="Currency" nillable="true" ... />
于 2020-08-27T05:57:00.103 回答