从不准确的WSDL

时间:2017-05-18 07:12:19

标签: java jaxb cxf

我正在从第三方提供的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"/>

1 个答案:

答案 0 :(得分:0)

如果只有日期字段有问题,您可以使用自定义绑定从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底部的常见问题解答部分。

相关问题