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