XJC - 忽略nillable = true

Cle*_*kod 5 java soap web-services jaxb xjc

为了避免包含值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>
Run Code Online (Sandbox Code Playgroud)

但是,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>
<!-- ... -->
Run Code Online (Sandbox Code Playgroud)

XJC将生成一个包含字段的类:

@XmlElement(required = true, nillable = true)
protected String fieldOne;
@XmlElement(nillable = true)
protected String fieldTwo;
Run Code Online (Sandbox Code Playgroud)

如果fieldTwo是,null那么请求将包含一个nil设置为true的字段:

<fieldOne>1001714</fieldOne>
<fieldTwo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
Run Code Online (Sandbox Code Playgroud)

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

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

小智 0

将 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>
Run Code Online (Sandbox Code Playgroud)

绑定.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>
Run Code Online (Sandbox Code Playgroud)