【问题标题】:XJC - ignore nillable=trueXJC - 忽略 nillable=true
【发布时间】:2016-11-17 14:42:17
【问题描述】:

为了避免值被包装在JAXBElement 中,我使用以下绑定文件从 XSD 生成类:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<jaxb:bindings
        xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" xmlns:xs="http://www.w3.org/2001/XMLSchema"
        xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/jaxb http://java.sun.com/xml/ns/jaxb/bindingschema_2_0.xsd"
        version="2.1">

    <jaxb:globalBindings generateElementProperty="false">
        <xjc:simple />
    </jaxb:globalBindings>

</jaxb:bindings>

但是,nillable 字段存在问题。对于以下 XSD:

<!-- ... -->
<xsd:complexType name="getShortCustomerIn">
  <xsd:sequence>
    <xsd:element name="fieldOne" nillable="true" type="xsd:string"/>
    <xsd:element name="fieldTwo" type="xsd:string" minOccurs="0"/>
  </xsd:sequence>
</xsd:complexType>
<!-- ... -->

XJC 将生成一个带有字段的类:

@XmlElement(required = true, nillable = true)
protected String fieldOne;
@XmlElement(nillable = true)
protected String fieldTwo;

fieldTwonull 时,请求将包含一个将 nil 设置为 true 的字段:

<fieldOne>1001714</fieldOne>
<fieldTwo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>

这对于我集成的 WebService 来说是个问题,因为它会在这样的请求上阻塞,尽管它是一个有效的 XML。我已经说过,当一个字段是null 时,它根本不应该出现在请求中。我意识到为了满足该要求,生成的类不能将@XmlElement 注释的nillable 属性设置为true

有没有办法在使用 XJC 生成类时忽略 XSD 文件中的 nillable=true

【问题讨论】:

  • 您是否有幸找到了解决方案?
  • 没有。在 XJC 中生成类时,我在 WSDL 输入上使用正则表达式替换。如果您希望我提供一个示例,请告诉我。
  • 天哪,我也遇到了同样的问题...我们可以在绑定文件中配置什么吗?
  • @Cleakod 你能帮忙解决你的正则表达式吗?
  • @JatinTelang - 我使用 Gradle 的任务(类型 Copy),它复制所有 WSDL 并使用 line.replaceAll 'nillable="true"', '' 过滤它们。请参阅here 了解更多信息。如果您需要进一步的帮助,请告诉我。

标签: java web-services soap jaxb xjc


【解决方案1】:

使用 XJC 和 jaxb2-basics-annotate 为我们工作

pom.xml

<plugin>
    <groupId>org.jvnet.jaxb2.maven2</groupId>
    <artifactId>maven-jaxb2-plugin</artifactId>
    <executions>
        <execution>
            <id>jaxb-generate</id>
            <phase>generate-sources</phase>
            <goals>
                <goal>generate</goal>
            </goals>
            <configuration>
                <schemaDirectory>src/main/schema</schemaDirectory>
                <bindingDirectory>src/main/schema/bindings</bindingDirectory>

                ...

                <args>
                    <arg>-Xannotate</arg>
                    <arg>-XremoveAnnotation</arg>
                </args>

                ...

                <plugins>
                    <plugin>
                        <groupId>org.jvnet.jaxb2_commons</groupId>
                        <artifactId>jaxb2-basics-annotate</artifactId>
                        <version>1.1.0</version>
                    </plugin>
                </plugins>
            </configuration>
        </execution>
    </executions>
</plugin>

bindings.xjb

<bindings schemaLocation="sample.xsd">
    <bindings multiple="true" node="//xsd:element[@nillable='true']">
        <annox:annotate target="field">@javax.xml.bind.annotation.XmlElement(nillable = false)</annox:annotate>          
    </bindings>
</bindings>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多