为了避免将值包含在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;
当fieldTwo
为null
时,请求将包含一个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
?
答案 0 :(得分: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>
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>