【问题标题】:Apache CXF Client generated from inaccurate WSDL从不准确的 WSDL 生成的 Apache CXF 客户端
【发布时间】:2017-05-20 15:13:51
【问题描述】:

我正在从第三方提供的 WSDL 创建 Apache CXF 客户端。当我在测试中使用 Web 服务时,我发现服务的输出不符合 WSDL。

到目前为止,对于某些元素,WSDL 似乎没有将它们定义为 nillable=true,而服务实际上确实使用 xsi:nil="true" 返回它们

我的 CXF 客户端无法解组返回的 xml 并崩溃。

我已经在一定程度上解决了它(通过编辑提供的 wsdl ),但我不认为这是一个可行的解决方案。第三方也拒绝将他们的服务和 wsdl 定义统一起来,称其他客户端在生产中正确处理了这一点。

那么,有没有办法让响应的解组变得不那么严格?我可以通过哪些其他途径来解决这个问题?

给我一​​个问题的字段之一的架构定义如下:

<xsd:element name="cardExpireDate" type="xsd:date"></xsd:element>

该字段由服务返回(在 SoapUI 中调用时)为 <cardExpireDate xsi:nil="true"/>

当我的代码尝试调用 Web 服务时,我收到以下错误(这在堆栈跟踪的下方,但我相信这是问题的根源)

Caused by: javax.xml.bind.UnmarshalException
 - with linked exception:
[com.sun.istack.SAXParseException2; lineNumber: 1; columnNumber: 1515; ]
    at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.handleStreamException(UnmarshallerImpl.java:483)
    at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:417)
    at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:394)
    at org.apache.cxf.jaxb.JAXBEncoderDecoder.doUnmarshal(JAXBEncoderDecoder.java:855)
    at org.apache.cxf.jaxb.JAXBEncoderDecoder.access$100(JAXBEncoderDecoder.java:102)

snip ....

Caused by: javax.xml.bind.UnmarshalException: 
 - with linked exception:
[java.lang.IllegalArgumentException: ]
    ... 92 more
Caused by: java.lang.IllegalArgumentException: 
    at org.apache.xerces.jaxp.datatype.XMLGregorianCalendarImpl$Parser.parseYear(Unknown Source)
    at org.apache.xerces.jaxp.datatype.XMLGregorianCalendarImpl$Parser.parse(Unknown Source)

我要解析的文档的第 1515 列是

<cardExpireDate xsi:nil="true"/>

【问题讨论】:

  • 您是否通过将 schema-validation-enabled 设置为 true 来打开 Schema 验证?然后只需将其关闭即可。这更像是一个 JAXB 而不是 CXF 问题。你能发布具体的例子和错误信息吗?
  • @DennisKieselhorst :我认为我没有打开模式验证 - 我只是使用默认值,我想我读到它默认是关闭的。我会用一些更具体的细节更新这个问题。

标签: java jaxb cxf


【解决方案1】:

如果只有日期字段有问题,您可以使用自定义绑定来使用 cxf-xjc-runtime 中的 org.apache.cxf.xjc.runtime.DataTypeAdapter:

<jaxws:bindings wsdlLocation="YOUR_WSDL_LOCATION"
          xmlns:jaxws="http://java.sun.com/xml/ns/jaxws"
          xmlns:xs="http://www.w3.org/2001/XMLSchema"
          xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
          xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
  <jaxws:bindings  node="wsdl:definitions/wsdl:types/xs:schema[@targetNamespace='THE_NAMESPACE_OF_YOUR_SCHEMA']">
      <jxb:globalBindings xmlns:jxb="http://java.sun.com/xml/ns/jaxb" xmlns:xs="http://www.w3.org/2001/XMLSchema">
        <jxb:javaType name="java.util.Date" xmlType="xs:dateTime"
                      parseMethod="org.apache.cxf.xjc.runtime.DataTypeAdapter.parseDateTime"
                      printMethod="org.apache.cxf.xjc.runtime.DataTypeAdapter.printDateTime"/>
      </jxb:globalBindings>
  </jaxws:bindings>
</jaxws:bindings>

如果您不知道如何应用绑定,请查看http://cxf.apache.org/docs/wsdl-to-java.html 底部的常见问题解答部分。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-15
    • 1970-01-01
    • 2013-08-20
    相关资源
    最近更新 更多